3.2 JAVA Multithreading & Multitasking
3.2 JAVA Multithreading & Multitasking
MULTITASKING IN JAVA
1. Introduction to Multithreading and Multitasking
1.1 What is Multithreading?
Multithreading is the concept in programming that allows a single program to perform multiple tasks
concurrently. In simple terms, it divides a program into smaller, independent units called threads, which
can execute simultaneously. This is especially useful in applications that need to perform multiple tasks,
such as downloading files while processing data.
• Efficient CPU Utilization: Multiple threads can run on multi-core processors, utilizing CPU
resources more effectively.
• Increased Responsiveness: For applications like GUIs, multithreading ensures that the
interface remains responsive while performing time-consuming operations in the background.
• Improved Resource Sharing: Threads can share resources within a process, making the
program more efficient.
1
2.3 Threads in Java
A thread in Java represents a single sequence of execution. Each thread has its own execution path, and
multiple threads can run concurrently within a single Java program. You can create a thread in two
ways:
• Extending the Thread class: By subclassing Thread and overriding the run() method.
• Implementing the Runnable interface: By implementing the Runnable interface and passing
it to a Thread object.
3. Understanding Multitasking
3.1 What is Multitasking?
Multitasking refers to the capability of a system to perform multiple tasks or processes simultaneously.
In a multitasking environment, the operating system allocates CPU time to each task, giving the illusion
that they are running at the same time.
2. Thread-Based Multitasking: This involves multiple threads within the same process, where
threads share the same memory space but can execute independently.
• Preemptive Multitasking: The OS forcibly takes control from a running task and allocates
CPU time to other tasks. This is more common in modern OS environments.
2
3.4 The Role of the Operating System
The operating system plays a key role in multitasking by managing the allocation of resources such as
memory, CPU time, and input/output devices. It ensures that tasks are performed efficiently, without
conflicts.
2. Runnable: The thread is ready to execute, and the JVM can schedule it.
• The thread may stay in the Timed Waiting state for a specified duration.
3
5.2 Methods of Synchronization
• Synchronized Methods: By using the synchronized keyword, you can ensure that only one
thread at a time can execute a particular method.
• Synchronized Blocks: You can synchronize specific sections of code, providing more granular
control over resource access.
• Deadlock: A situation where two or more threads are blocked indefinitely, waiting for each
other to release resources.
this.balance = balance;
balance += amount;
notify();
4
try
wait();
catch (InterruptedException e)
e.printStackTrace();
balance -= amount;
return balance;
this.account = account;
this.operation = operation;
this.amount = amount;
@OVERRIDE
if ("deposit".equalsIgnoreCase(operation))
account.deposit(amount);
5
}
else if ("withdraw".equalsIgnoreCase(operation))
account.withdraw(amount);
customer1.start();
customer2.start();
customer3.start();
try
customer1.join();
customer2.join();
customer3.join();
catch (InterruptedException e)
e.printStackTrace();
Sample Output:
Customer-0 waiting for balance.
Balance: Rs.2000
6
Balance: Rs.1500
Balance: Rs.0
• Multithreading is more efficient for sharing resources within the same process.
• Use Thread Pools: For managing a large number of threads, use ExecutorService to manage
thread pools efficiently.
• Not joining threads before exiting the program, leading to incomplete execution.