SlideShare a Scribd company logo
1 of 48
Chapter Four
Multithreading Concept
Introduction
• Multithreading in java is a process of executing multiple threads
simultaneously. Or in other words,
• Multithreading enables multiple tasks in a program to be executed
concurrently.
• Multiprocessor: refers to the use of two or more central processing units
(CPU) within a single computer system.
• Multiprocessing and multithreading, both are used to achieve multitasking.
• Java Multithreading is mostly used in games, animation, etc.
• Multitasking is when multiple processes share common processing resources
such as a CPU.
• Multi-threading extends the idea of multitasking into applications where you
can subdivide specific operations within a single application into individual
threads.
• Each of the threads can run in parallel.
1
Multitasking
• Multitasking is a process of executing multiple tasks
simultaneously. We use multitasking to utilize the CPU.
• Multitasking can be achieved in two ways:
1. Process-based Multitasking (Multiprocessing)
2. Thread-based Multitasking (Multithreading)
1. Process-based Multitasking (Multiprocessing)
– Each process has an address in memory. In other words, each
process allocates a separate memory area.
– A process is heavyweight.
– Cost of communication between the process is high.
– Switching from one process to another requires some time for
saving and loading registers, memory maps, updating lists, etc.
2. Thread-based Multitasking (Multithreading)
– Threads share the same address space.
– A thread is lightweight.
– Cost of communication between the thread is low.
2
Advantages of Multithreading
• You can perform multiple operations together at a time,
so it saves time
• Threads are independent, so it doesn't affect other
threads if an exception occurs in a single thread.
• It is more responsive to user input – GUI application can
interrupt a time-consuming task.
• Server can handle multiple clients simultaneously
• It is used to perform tasks in parallel processing
• A thread can execute concurrently with other threads
within a single process.
• All threads managed by the JVM share memory space
and can communicate with each other.
3
Process Vs Thread
4
Process Thread
Process is a program in
execution.
A thread is a subset (part) of the process.
A process consists of multiple
threads.
A thread is a smallest part of the process that
can execute concurrently with other parts
(threads) of the process.
A process is a heavyweight
program
A thread is a lightweight program.
A process has its own address
space.
A thread uses the process’s address space and
shares it with the other threads of that process.
A process can communicate with
other process by using inter-
process communication.
A thread can communicate with other thread
(of the same process) directly by using
methods like wait(), notify(), notifyAll().
A process does not have control
over the sibling process; it has
control over its child processes
only.
Threads have control over the other threads of
the same process.
What is Thread in Java?
• A thread is a lightweight sub-process, the smallest unit of
processing.
• It is a separate path of execution.
• Threads are independent. If there occurs exception in one
thread, it doesn't affect other threads.
• It uses a shared memory area.
• Thread: single sequential flow of control within a
program
• Single-threaded program can handle one task at any time.
• Multitasking allows single processor to run several
concurrent threads.
• Most modern operating systems support multitasking.
5
What is Thread in Java?
• A program may consist of many tasks that can run
concurrently.
• A thread is the flow of execution, from beginning to
end, of a task.
• A thread provides the mechanism for running a task.
• With Java, you can launch multiple threads from a
program concurrently.
• These threads can be executed simultaneously in
multiprocessor systems, as shown in Figure below.
6
Multiple threads
are running on
multiple CPUs
Multiple threads
sharing a single
CPU
What is Thread in Java?
7
The Thread class in Java
• Java provides Thread class to achieve thread programming.
• Thread class provides constructors and methods to create and
perform operations on a thread.
• Threads can be created by using two mechanisms :
1. Extending java.lang.Thread class
 The thread class extends the Thread class
 run() method must be overridden
 run() is called when execution of the thread begins
 A thread terminates when run() returns
 start() method invokes run()
 Calling run() does not create a new thread
2. Implementing java.lang.Runnable interface
 Thread class implements Runnable interface.
 Runnable interface have only one method named: public void run():
is used to perform action for a thread.
 You need to implement a run() method provided the Runnable
interface
8
// Custom task class
public class TaskClass implements Runnable {
...
public TaskClass(...) {
...
}
// Implement the run method in Runnable
public void run() {
// Tell system how to run custom thread
...
}
...
}
// Client class
public class Client {
...
public void someMethod() {
...
// Create an instance of TaskClass
TaskClass task = new TaskClass(...);
// Create a thread
Thread thread = new Thread(task);
// Start a thread
thread.start();
...
}
...
}
java.lang.Runnable TaskClass
1. Creating a Thread by implementing Runnable Interface
• We create a new class which implements Runnable interface and
override run() method.
• Then we instantiate a Thread object and call start() method on
this object.
9
Example: Creating and Launching Threads using
the Runnable Interface
• The program in the next slide creates/performs
three tasks and runs three threads:
▫ The first thread prints the letter a 100 times.
▫ The second thread prints the letter b 100 times.
▫ The third thread prints the integers 1 through 100.
10
public class TaskThreadDemo {
public static void main(String[] args) {
// Create tasks
Runnable printA = new PrintChar('a' , 100);
Runnable printB = new PrintChar('b' , 100);
Runnable print100 = new PrintNum(100);
// Create threads
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);
// Start threads
thread1.start();
thread2.start();
thread3.start();
}
}
Example: Using the Runnable Interface
11
class PrintChar implements Runnable {
private char charToPrint;
private int times;
public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}
public void run() {
for (int i = 0; i < times; i++) {
System.out.print(charToPrint);
}
}
}
class PrintNum implements Runnable{
private int lastNum;
public PrintNum(int n) {
lastNum = n;
}
public void run() {
for (int i = 1; i <= lastNum; i++) {
System.out.print(" " + i);
}
}
}
Example: Using the Runnable Interface
12
Code Description
• The program creates three tasks .
• To run them concurrently, three threads are created.
• The start() method is invoked to start a thread that causes the run()
method in the task to be executed.
• When the run() method completes, the thread terminates.
• Because the first two tasks, printA and printB, have similar
functionality, they can be defined in one task class PrintChar.
• The PrintChar class implements Runnable and overrides the run()
method with the print-character action.
• This class provides a framework for printing any single character a
given number of times.
• The runnable objects printA and printB are instances of the PrintChar
class.
• The PrintNum class implements Runnable and overrides the run()
method with the print-number action.
• This class provides a framework for printing numbers from 1 to n, for
any integer n. The runnable object print100 is an instance of the class
printNum class.
13
Run() Methods
• The run() method in a task specifies how to
perform the task.
• This method is automatically invoked by the
JVM.
• You should not invoke it.
• Invoking run() directly merely executes this
method in the same thread; no new thread is
started.
14
• We create a class that extends
the java.lang.Thread class.
• This class overrides the run() method available in
the Thread class.
• A thread begins its life inside run() method.
• We create an object of our new class and call start()
method to start the execution of a thread.
• Start() invokes the run() method on the Thread
object.
15
2. Creating a Thread by extending Thread class
2. Creating a Thread by extending Thread class
The Thread class contains the constructors for
creating threads for tasks and the methods for
controlling threads.
16
17
2. Creating a Thread by extending Thread class
The static yield() Method
You can use the yield() method to temporarily
release time for other threads.
For example, suppose you modify the code in the
run() method inTaskThreadDemo.java as follows:
public void run() {
for (int i = 1; i <= lastNum; i++) {
System.out.print(" " + i);
Thread.yield();
}
}
Every time a number is printed, the print100 thread
is yielded. So, the numbers are printed after the
characters.
18
The static sleep(milliseconds) Method
The sleep(long milliseconds) method of Thread class is used to sleep
a thread for the specified amount of time.
For example, suppose you modify the code in TaskThreadDemo.java
as follows:
public void run() {
for (int i = 1; i <= lastNum; i++) {
System.out.print(" " + i);
try {
if (i >= 50)
Thread.sleep(1);
}
catch (InterruptedException ex) {
}
}
}
Every time a number (>= 50) is printed, the print100 thread is put to
sleep for 1 millisecond.
19
The join() Method
You can use the join() method to force one thread to wait for another thread
to finish.
For example, suppose you modify the code in TaskThreadDemo.java as
follows:
The numbers after 50 are printed after thread printA is finished.
printA.join()
-char token
+getToken
+setToken
+paintCompone
t
+mouseClicked
Thread
print100
-char token
+getToken
+setToken
+paintCompo
net
+mouseClicke
d
Wait for printA
to finish
+getToken
+setToken
+paintComponet
Thread
printA
-char token
+getToken
+setToken
+paintCompo
net
+mouseClicke
d
printA finished
-char token
public void run() {
Thread thread4 = new Thread(
new PrintChar('c', 40));
thread4.start();
try {
for (int i = 1; i <= lastNum; i++) {
System.out.print(" " + i);
if (i == 50) thread4.join();
}
}
catch (InterruptedException ex) {
}
}
20
Life Cycle of a Thread (Thread States)
New Ready
Thread created
Finished
Running
start()
run()
Wait for target
to finish
join()
run() returns
yield(), or
time out
interrupt()
Wait for time
out
Wait to be
notified
sleep()
wait()
Target
finished
notify() or
notifyAll()
Time out
Blocked
Interrupted()
• A thread state indicates the status of thread.
• Tasks are executed in threads.
• A thread can be in one of five states: New, Ready/Runnable,
Running, Blocked, or Finished/Terminated.
• The life cycle of the thread in java is controlled by JVM.
21
• When a thread is newly created, it enters the New state.
• After a thread is started by calling its start() method, it
enters the Ready/Runnable state.
• A ready thread is runnable but may not be running yet.
• The operating system has to allocate CPU time to it.
• When a ready thread begins executing, it enters the
Running state.
• A running thread can enter the Ready state if its given CPU
time expires or its yield() method is called.
• A thread can enter the Blocked state (i.e., become inactive)
for several reasons.
• It may have invoked the join() , sleep() , or wait() method.
• It may be waiting for an I/O operation to finish.
22
Life Cycle of a Thread (Thread States)
• A blocked thread may be reactivated when the
action inactivating it is reversed.
• For example, if a thread has been put to sleep and
the sleep time has expired, the thread is
reactivated and enters the Ready state.
• Finally, a thread is Finished if it completes the
execution of its run() method.
23
Life Cycle of a Thread (Thread States)
Thread methods
 isAlive()
 method used to find out the state of a thread.
 returns true: thread is in the Ready, Blocked, or Running state
 returns false: thread is new and has not started or if it is finished.
 interrupt()
 if a thread is currently in the Ready or Running state, its interrupted
flag is set;
 if a thread is currently blocked, it is awakened and enters the Ready
state, and an java.io.InterruptedException is thrown.
 The isInterrupt() method tests whether the thread is
interrupted.
24
The deprecated stop(), suspend(), and resume()
Methods
NOTE: The Thread class also contains the stop(), suspend(), and
resume() methods.
As of Java 2, these methods are deprecated (or outdated) because
they are known to be inherently unsafe.
You should assign null to a Thread variable to indicate that it is
stopped rather than use the stop() method.
25
Thread Priority
• Each thread have a priority. Priorities are represented by numbers ranging from 1
and 10.
• You can increase or decrease the priority of any thread by using the
setPriority method, and you can get the thread’s priority by using the getPriority
method.
• The Thread class has three int constant priorities.
– public static int MIN_PRIORITY --- with value 1
– public static int NORM_PRIORITY --- with value 5
– public static int MAX_PRIORITY --- with value 10
• Default priority of a thread is 5 (NORM_PRIORITY).
• The JVM always picks the currently runnable thread with the highest priority.
• A lower priority thread can run only when no higher-priority threads are running.
• If all runnable threads have equal priorities, each is assigned an equal portion of the
CPU time in a circular queue. This is called round-robin scheduling.
• You can reset the priority using setPriority(int priority).
• For example, suppose you insert the following code in TaskThreadDemo.java for
thread3. thread3.setPriority(Thread.MAX_PRIORITY);
• The thread for the print100 task will be finished first. 26
Thread Scheduling in Java
• An operating system’s thread scheduler determines which thread runs
next.
• Most operating systems use time slicing for threads of equal priority.
• Thread scheduler in java is the part of the JVM that decides which
thread should run.
• There is no guarantee that which runnable thread will be chosen to run by
the thread scheduler.
• Only one thread at a time can run in a single process.
• The thread scheduler mainly uses preemptive or time slicing scheduling
to schedule the threads.
• Preemptive scheduling: the highest priority task executes until it enters a
higher priority task comes into existence. when a thread of higher priority
enters the running state, it preempts the current thread.
• Time slicing: a task executes for a predefined slice of time and then
reenters the pool of ready tasks.
27
Thread Scheduling in Java
• Starvation: Higher-priority threads can postpone (possible
forever) the execution of lower-priority threads.
• A thread may never get a chance to run if there is always a
higher-priority thread running or a same-priority thread
that never yields.
• This situation is known as contention or starvation.
• To avoid contention, the thread with higher priority must
periodically invoke the sleep or yield method to give a
thread with a lower or the same priority
a chance to run.
28
Java Thread Pool
29
• Thread pool can be used to execute tasks efficiently
• Defining tasks and threads independently is convenient for a
single task execution, but it is not efficient for a large number
of tasks, because you have to create a thread for each task.
• Starting a new thread for each task could limit throughput and
cause poor performance.
• A thread pool is ideal to manage the number of tasks executing
concurrently.
• A thread pool reuses previously created threads to execute
current tasks and offers a solution to the problem of thread
cycle overhead and resource thrashing.
• Java provides the Executor interface for executing tasks in a
thread pool and the ExecutorService interface for managing
and controlling tasks.
• ExecutorService is a sub-interface of Executor, as
shown in Figure below.
Shuts down the executor, but allows the tasks in the executor to
complete. Once shutdown, it cannot accept new tasks.
Shuts down the executor immediately even though there are
unfinished threads in the pool. Returns a list of unfinished
tasks.
Returns true if the executor has been shutdown.
Returns true if all tasks in the pool are terminated.
ÂŤinterfaceÂť
java.util.concurrent.Executor
+execute(Runnable object): void Executes the runnable task.

ÂŤinterfaceÂť
java.util.concurrent.ExecutorService
+shutdown(): void
+shutdownNow(): List<Runnable>
+isShutdown(): boolean
+isTerminated(): boolean
Java Thread Pool
30
Creating Executors
To create an Executor object, use the static methods in the Executors class
as shown in Figure below. The newFixedThreadPool(int) method creates
a fixed number of threads in a pool. If a thread completes executing a task,
it can be reused to execute another task. If a thread terminates due to a
failure prior to shutdown, a new thread will be created to replace it if all the
threads in the pool are not idle and there are tasks waiting for execution.
The newCachedThreadPool() method creates a new thread if all the
threads in the pool are not idle and there are tasks waiting for execution. A
thread in a cached pool will be terminated if it has not been used for 60
seconds. A cached pool is efficient for many short tasks.
Creates a thread pool with a fixed number of threads executing
concurrently. A thread may be reused to execute another task
after its current task is finished.
Creates a thread pool that creates new threads as needed, but
will reuse previously constructed threads when they are
available.
java.util.concurrent.Executors
+newFixedThreadPool(numberOfThreads:
int): ExecutorService
+newCachedThreadPool():
ExecutorService
31
Example: Thread Pool
import java.util.concurrent.*;
public class ExecutorDemo {
public static void main(String[] args) {
// Create a fixed thread pool with maximum three threads
ExecutorService executor = Executors.newFixedThreadPool(3);
// Submit runnable tasks to the executor
executor.execute( new PrintChar('a' , 100));
executor.execute(new PrintChar(‘b' , 100));
executor.execute(new PrintNum(100));
// Shut down the executor
executor.shutdown();
}
}
 The following program shows how to rewrite the code in TaskThreadDemo.java using
a thread pool.
 Steps to be follow:
1. Create a task (Runnable object) to execute
2. Create Executor Pool using Executors
3. Pass tasks to Executor Pool
4. Shutdown the Executor Pool
32
Thread Synchronization
• Thread Synchronization is the capability to control the
access of multiple threads to any shared resource.
• Java Synchronization is better option where we want to
allow only one thread to access the shared resource.
• A shared resource may be corrupted if it is accessed
simultaneously by multiple threads.
• Example: two unsynchronized threads accessing the same
bank account may cause conflict. Task 1 and Task 2 both
add 1 to the same balance
33
Example: Showing Resource Conflict
• Objective: Write a program that demonstrates the problem of
resource conflict. Suppose that you create and launch one
hundred threads, each of which adds a penny to an account.
Assume that the account is initially empty.
34
import java.util.concurrent.*;
public class AccountWithoutSync {
private static Account account = new Account();
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 100; i++) {
executor.execute(new AddAPennyTask());
}
executor.shutdown();
while (!executor.isTerminated() ) {
}
System.out.println("What is balance? " + account.getBalance());
}
private static class AddAPennyTask implements Runnable {
public void run() {
account.deposit(1);
}
}
private static class Account {
private int balance = 0;
public int getBalance() {
return balance;
}
public void deposit(int amount) {
int newBalance = balance + amount;
try {
Thread.sleep(5);
}
catch (InterruptedException ex) {
}
balance = newBalance;
}
}
}
Example: Showing Resource Conflict
35
Race Condition
What, then, caused the error in the example? Here is a possible
scenario:
Step balance Task 1 Task 2
1 0 newBalance = balance + 1;
2 0 newBalance = balance + 1;
3 1 balance = newBalance;
4 1 balance = newBalance;
);
• Effect: Task 1 did nothing (in Step 4 Task 2 overrides the result)
• Problem: Task 1 and Task 2 are accessing a common resource in a
way that causes conflict.
• Known as a race condition in multithreaded programs.
•A thread-safe class does not cause a race condition in the presence of
multiple threads. The Account class is not thread-safe.
36
The synchronized Keyword
37
• Problem: race conditions
• Solution: give exclusive access to one thread at a time to code that
manipulates a shared object.
• To avoid race conditions, it is necessary to prevent more than one thread
from simultaneously entering a certain part of the program, known as
the critical region.
• Synchronization keeps other threads waiting until the object is available.
• The synchronized keyword synchronizes the method so that only one
thread can access the method at a time.
• The critical region in the previous code (AccountWithoutSync.java) is
the entire deposit method.
• One way to correct the problem in this code: make Account thread-safe
by adding the synchronized keyword in deposit method as follows:
public synchronized void deposit(double amount)
Synchronizing Instance Methods and Static Methods
38
• A synchronized method acquires a lock before it executes.
• A lock is a mechanism for exclusive use of a resource.
• Instance method: the lock is on the object for which it was
invoked.
• Invoking a synchronized instance method of an object
acquires a lock on the object.
• Static method: the lock is on the class.
• Invoking a synchronized static method of a class acquires a
lock on the class.
• If one thread invokes a synchronized instance method
(respectively, static method) on an object, the lock of that
object (respectively, class) is acquired, then the method is
executed, and finally the lock is released.
• Another thread invoking the same method of that object
(respectively, class) is blocked until the lock is released.
With the deposit method synchronized, the preceding scenario cannot
happen.
If Task 2 starts to enter the method, and Task 1 is already in the
method, Task 2 is blocked until Task 1 finishes the method.
Acquire a lock on the object account
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
Execute the deposit method
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
Release the lock
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
Task 1
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
Acqurie a lock on the object account
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
Execute the deposit method
-char token
+getToken
+setToken
+paintComponet
Release the lock
Task 2
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
Wait to acquire the lock
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
39
Synchronizing Instance Methods and Static Methods
Synchronized Block
• Synchronized block can be used to perform synchronization on any specific
resource of the method.
• Suppose you have 50 lines of code in your method, but you want to synchronize
only 5 lines, you can use synchronized block.
• Synchronized block is used to lock an object for any shared resource.
• Scope of synchronized block is smaller than the method.
synchronized (object reference expression) {
statements;
}
• If the object is already locked by another thread, the thread is blocked until the
lock is released.
• When a lock is obtained on the object, the statements in the synchronized block
are executed, and then the lock is released.
• Synchronized statements enable you to synchronize part of the code in a method
instead of the entire method. This increases concurrency.
• You can make AccountWithoutSync.java thread-safe by placing the statement
inside a synchronized block:
synchronized (account) {
account.deposit(1);
}
40
Inter-Thread Communication (Cooperation Among Threads)
• Inter-thread communication or Co-operation is all about allowing
synchronized threads to communicate with each other.
• Cooperation (Inter-thread communication) is a mechanism in which a
thread is paused running in its critical section and another thread is
allowed to enter (or lock) in the same critical section to be executed. It is
implemented by following methods of Object class:
• wait(): this method lets the thread wait until some condition occurs.
• notify(): this method wakes up only one thread from a waiting queue.
• notifyAll(): this method wakes up all waiting threads.
• Use the wait(), notify(), and notifyAll() methods to facilitate
communication among threads.
• The wait(), notify(), and notifyAll() methods must be called in a
synchronized method or a synchronized block on the calling object of
these methods. Otherwise, an IllegalMonitorStateException would occur.
• Conditions can be used for communication among threads.
41
• Example: To demonstrate thread communications.
• Suppose that you create and launch two tasks:
1. one that deposits into an account, and
2. one that withdraws from the same account.
• The withdraw task has to wait if the amount to be
withdrawn is more than the current balance.
• Whenever new funds are deposited into the account, the
deposit task notifies the withdraw thread to resume.
• If the amount is still not enough for a withdrawal, the
withdraw thread has to continue to wait for a new deposit.
42
Inter-Thread Communication (Cooperation Among Threads)
while (balance < withdrawAmount)
newDeposit.await();
Withdraw Task
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
balance -= withdrawAmount
-char token
+getToken
+setToken
lock.unlock();
Deposit Task
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
lock.lock();
-char token
+getToken
+setToken
+paintComponet
+mouseClicked newDeposit.signalAll();
balance += depositAmount
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
lock.unlock();
-char token
lock.lock();
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
43
Inter-Thread Communication (Cooperation Among Threads)
• To synchronize the operations, use a lock with a condition
to synchronize operations: newDeposit
• If the balance is less than the amount to be withdrawn, the
withdraw task will wait for the newDeposit condition.
• When the deposit task adds money to the account, the task
signals the waiting withdraw task to try again.
• Interaction between the two tasks:
Semaphores
Acquire a permit from a semaphore.
Wait if the permit is not available.
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
A thread accessing a shared resource
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
Access the resource
-char token
+getToken
+setToken
+paintComponet
Release the permit to the semaphore
-char token
semaphore.acquire();
A thread accessing a shared resource
-char token
+getToken
+setToken
+paintComponet
+mouseClicked
Access the resource
-char token
+getToken
+setToken
+paintComponet
semaphore.release();
-char token
44
• Semaphores can be used to restrict the number of threads that access a
shared resource.
• A semaphore is an object that controls the access to a common resource.
• Before accessing the resource, a thread must acquire a permit from the
semaphore.
• After finishing with the resource, the thread must return the permit back
to the semaphore, as shown in Figure below.
Creating Semaphores
Creates a semaphore with the specified number of permits. The
fairness policy is false.
Creates a semaphore with the specified number of permits and
the fairness policy.
Acquires a permit from this semaphore. If no permit is
available, the thread is blocked until one is available.
Releases a permit back to the semaphore.
java.util.concurrent.Semaphore
+Semaphore(numberOfPermits: int)
+Semaphore(numberOfPermits: int, fair:
boolean)
+acquire(): void
+release(): void
45
• To create a semaphore, you have to specify the number of permits with
an optional fairness policy, as shown in Figure below.
• A task acquires a permit by invoking the semaphore’s acquire() method
and releases the permit by invoking the semaphore’s release() method.
• Once a permit is acquired, the total number of available permits in a
semaphore is reduced by 1.
• Once a permit is released, the total number of available permits in a
semaphore is increased by 1.
Deadlock
46
• Deadlock in java is a part of multithreading. Deadlock can occur in a
situation when a thread is waiting for an object lock, that is acquired by
another thread and second thread is waiting for an object lock that is
acquired by first thread. Since, both threads are waiting for each other to
release the lock, the condition is called deadlock.
• Consider the scenario with two threads and two objects, as shown in
Figure below. Thread 1 has acquired a lock on object1, and Thread 2 has
acquired a lock on object2. Now Thread 1 is waiting for the lock on
object2, and Thread 2 for the lock on object1. Each thread waits for the
other to release the lock it needs, and until that happens, neither can
continue to run.
Preventing Deadlock
47
• Deadlocks can be easily avoided by using a proper
resource ordering.
• With this technique, assign an order on all the objects
whose locks must be acquired and ensure that the
locks are acquired in that order.
• For the above example, suppose that the objects are
ordered as object1 and object2.
• Using the resource ordering technique, Thread 2 must
acquire a lock on object1 first, then on object2.
• Once Thread 1 acquires a lock on object1, Thread 2
has to wait for a lock on object1. Thus, Thread 1 will
be able to acquire a lock on object2 and no deadlock
will occur.
The End!!
48

More Related Content

Similar to Multithreading.pptx

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
Java multithreading
Java multithreadingJava multithreading
Java multithreadingMohammed625
 
multithreading
multithreadingmultithreading
multithreadingRajkattamuri
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxArulmozhivarman8
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfgiridharsripathi
 
Multithreading
MultithreadingMultithreading
MultithreadingF K
 
multithreadingppt.pptx
multithreadingppt.pptxmultithreadingppt.pptx
multithreadingppt.pptxFardeenAzhar
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10Minal Maniar
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Threads in Java
Threads in JavaThreads in Java
Threads in JavaHarshaDokula
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024nehakumari0xf
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024kashyapneha2809
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadKartik Dube
 

Similar to Multithreading.pptx (20)

Java
JavaJava
Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
multithreading
multithreadingmultithreading
multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdf
 
Java
JavaJava
Java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
multithreadingppt.pptx
multithreadingppt.pptxmultithreadingppt.pptx
multithreadingppt.pptx
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 

Recently uploaded

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 

Recently uploaded (20)

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 

Multithreading.pptx

  • 1. Chapter Four Multithreading Concept Introduction • Multithreading in java is a process of executing multiple threads simultaneously. Or in other words, • Multithreading enables multiple tasks in a program to be executed concurrently. • Multiprocessor: refers to the use of two or more central processing units (CPU) within a single computer system. • Multiprocessing and multithreading, both are used to achieve multitasking. • Java Multithreading is mostly used in games, animation, etc. • Multitasking is when multiple processes share common processing resources such as a CPU. • Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. • Each of the threads can run in parallel. 1
  • 2. Multitasking • Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. • Multitasking can be achieved in two ways: 1. Process-based Multitasking (Multiprocessing) 2. Thread-based Multitasking (Multithreading) 1. Process-based Multitasking (Multiprocessing) – Each process has an address in memory. In other words, each process allocates a separate memory area. – A process is heavyweight. – Cost of communication between the process is high. – Switching from one process to another requires some time for saving and loading registers, memory maps, updating lists, etc. 2. Thread-based Multitasking (Multithreading) – Threads share the same address space. – A thread is lightweight. – Cost of communication between the thread is low. 2
  • 3. Advantages of Multithreading • You can perform multiple operations together at a time, so it saves time • Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread. • It is more responsive to user input – GUI application can interrupt a time-consuming task. • Server can handle multiple clients simultaneously • It is used to perform tasks in parallel processing • A thread can execute concurrently with other threads within a single process. • All threads managed by the JVM share memory space and can communicate with each other. 3
  • 4. Process Vs Thread 4 Process Thread Process is a program in execution. A thread is a subset (part) of the process. A process consists of multiple threads. A thread is a smallest part of the process that can execute concurrently with other parts (threads) of the process. A process is a heavyweight program A thread is a lightweight program. A process has its own address space. A thread uses the process’s address space and shares it with the other threads of that process. A process can communicate with other process by using inter- process communication. A thread can communicate with other thread (of the same process) directly by using methods like wait(), notify(), notifyAll(). A process does not have control over the sibling process; it has control over its child processes only. Threads have control over the other threads of the same process.
  • 5. What is Thread in Java? • A thread is a lightweight sub-process, the smallest unit of processing. • It is a separate path of execution. • Threads are independent. If there occurs exception in one thread, it doesn't affect other threads. • It uses a shared memory area. • Thread: single sequential flow of control within a program • Single-threaded program can handle one task at any time. • Multitasking allows single processor to run several concurrent threads. • Most modern operating systems support multitasking. 5
  • 6. What is Thread in Java? • A program may consist of many tasks that can run concurrently. • A thread is the flow of execution, from beginning to end, of a task. • A thread provides the mechanism for running a task. • With Java, you can launch multiple threads from a program concurrently. • These threads can be executed simultaneously in multiprocessor systems, as shown in Figure below. 6
  • 7. Multiple threads are running on multiple CPUs Multiple threads sharing a single CPU What is Thread in Java? 7
  • 8. The Thread class in Java • Java provides Thread class to achieve thread programming. • Thread class provides constructors and methods to create and perform operations on a thread. • Threads can be created by using two mechanisms : 1. Extending java.lang.Thread class  The thread class extends the Thread class  run() method must be overridden  run() is called when execution of the thread begins  A thread terminates when run() returns  start() method invokes run()  Calling run() does not create a new thread 2. Implementing java.lang.Runnable interface  Thread class implements Runnable interface.  Runnable interface have only one method named: public void run(): is used to perform action for a thread.  You need to implement a run() method provided the Runnable interface 8
  • 9. // Custom task class public class TaskClass implements Runnable { ... public TaskClass(...) { ... } // Implement the run method in Runnable public void run() { // Tell system how to run custom thread ... } ... } // Client class public class Client { ... public void someMethod() { ... // Create an instance of TaskClass TaskClass task = new TaskClass(...); // Create a thread Thread thread = new Thread(task); // Start a thread thread.start(); ... } ... } java.lang.Runnable TaskClass 1. Creating a Thread by implementing Runnable Interface • We create a new class which implements Runnable interface and override run() method. • Then we instantiate a Thread object and call start() method on this object. 9
  • 10. Example: Creating and Launching Threads using the Runnable Interface • The program in the next slide creates/performs three tasks and runs three threads: ▫ The first thread prints the letter a 100 times. ▫ The second thread prints the letter b 100 times. ▫ The third thread prints the integers 1 through 100. 10
  • 11. public class TaskThreadDemo { public static void main(String[] args) { // Create tasks Runnable printA = new PrintChar('a' , 100); Runnable printB = new PrintChar('b' , 100); Runnable print100 = new PrintNum(100); // Create threads Thread thread1 = new Thread(printA); Thread thread2 = new Thread(printB); Thread thread3 = new Thread(print100); // Start threads thread1.start(); thread2.start(); thread3.start(); } } Example: Using the Runnable Interface 11
  • 12. class PrintChar implements Runnable { private char charToPrint; private int times; public PrintChar(char c, int t) { charToPrint = c; times = t; } public void run() { for (int i = 0; i < times; i++) { System.out.print(charToPrint); } } } class PrintNum implements Runnable{ private int lastNum; public PrintNum(int n) { lastNum = n; } public void run() { for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); } } } Example: Using the Runnable Interface 12
  • 13. Code Description • The program creates three tasks . • To run them concurrently, three threads are created. • The start() method is invoked to start a thread that causes the run() method in the task to be executed. • When the run() method completes, the thread terminates. • Because the first two tasks, printA and printB, have similar functionality, they can be defined in one task class PrintChar. • The PrintChar class implements Runnable and overrides the run() method with the print-character action. • This class provides a framework for printing any single character a given number of times. • The runnable objects printA and printB are instances of the PrintChar class. • The PrintNum class implements Runnable and overrides the run() method with the print-number action. • This class provides a framework for printing numbers from 1 to n, for any integer n. The runnable object print100 is an instance of the class printNum class. 13
  • 14. Run() Methods • The run() method in a task specifies how to perform the task. • This method is automatically invoked by the JVM. • You should not invoke it. • Invoking run() directly merely executes this method in the same thread; no new thread is started. 14
  • 15. • We create a class that extends the java.lang.Thread class. • This class overrides the run() method available in the Thread class. • A thread begins its life inside run() method. • We create an object of our new class and call start() method to start the execution of a thread. • Start() invokes the run() method on the Thread object. 15 2. Creating a Thread by extending Thread class
  • 16. 2. Creating a Thread by extending Thread class The Thread class contains the constructors for creating threads for tasks and the methods for controlling threads. 16
  • 17. 17 2. Creating a Thread by extending Thread class
  • 18. The static yield() Method You can use the yield() method to temporarily release time for other threads. For example, suppose you modify the code in the run() method inTaskThreadDemo.java as follows: public void run() { for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); Thread.yield(); } } Every time a number is printed, the print100 thread is yielded. So, the numbers are printed after the characters. 18
  • 19. The static sleep(milliseconds) Method The sleep(long milliseconds) method of Thread class is used to sleep a thread for the specified amount of time. For example, suppose you modify the code in TaskThreadDemo.java as follows: public void run() { for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); try { if (i >= 50) Thread.sleep(1); } catch (InterruptedException ex) { } } } Every time a number (>= 50) is printed, the print100 thread is put to sleep for 1 millisecond. 19
  • 20. The join() Method You can use the join() method to force one thread to wait for another thread to finish. For example, suppose you modify the code in TaskThreadDemo.java as follows: The numbers after 50 are printed after thread printA is finished. printA.join() -char token +getToken +setToken +paintCompone t +mouseClicked Thread print100 -char token +getToken +setToken +paintCompo net +mouseClicke d Wait for printA to finish +getToken +setToken +paintComponet Thread printA -char token +getToken +setToken +paintCompo net +mouseClicke d printA finished -char token public void run() { Thread thread4 = new Thread( new PrintChar('c', 40)); thread4.start(); try { for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); if (i == 50) thread4.join(); } } catch (InterruptedException ex) { } } 20
  • 21. Life Cycle of a Thread (Thread States) New Ready Thread created Finished Running start() run() Wait for target to finish join() run() returns yield(), or time out interrupt() Wait for time out Wait to be notified sleep() wait() Target finished notify() or notifyAll() Time out Blocked Interrupted() • A thread state indicates the status of thread. • Tasks are executed in threads. • A thread can be in one of five states: New, Ready/Runnable, Running, Blocked, or Finished/Terminated. • The life cycle of the thread in java is controlled by JVM. 21
  • 22. • When a thread is newly created, it enters the New state. • After a thread is started by calling its start() method, it enters the Ready/Runnable state. • A ready thread is runnable but may not be running yet. • The operating system has to allocate CPU time to it. • When a ready thread begins executing, it enters the Running state. • A running thread can enter the Ready state if its given CPU time expires or its yield() method is called. • A thread can enter the Blocked state (i.e., become inactive) for several reasons. • It may have invoked the join() , sleep() , or wait() method. • It may be waiting for an I/O operation to finish. 22 Life Cycle of a Thread (Thread States)
  • 23. • A blocked thread may be reactivated when the action inactivating it is reversed. • For example, if a thread has been put to sleep and the sleep time has expired, the thread is reactivated and enters the Ready state. • Finally, a thread is Finished if it completes the execution of its run() method. 23 Life Cycle of a Thread (Thread States)
  • 24. Thread methods  isAlive()  method used to find out the state of a thread.  returns true: thread is in the Ready, Blocked, or Running state  returns false: thread is new and has not started or if it is finished.  interrupt()  if a thread is currently in the Ready or Running state, its interrupted flag is set;  if a thread is currently blocked, it is awakened and enters the Ready state, and an java.io.InterruptedException is thrown.  The isInterrupt() method tests whether the thread is interrupted. 24
  • 25. The deprecated stop(), suspend(), and resume() Methods NOTE: The Thread class also contains the stop(), suspend(), and resume() methods. As of Java 2, these methods are deprecated (or outdated) because they are known to be inherently unsafe. You should assign null to a Thread variable to indicate that it is stopped rather than use the stop() method. 25
  • 26. Thread Priority • Each thread have a priority. Priorities are represented by numbers ranging from 1 and 10. • You can increase or decrease the priority of any thread by using the setPriority method, and you can get the thread’s priority by using the getPriority method. • The Thread class has three int constant priorities. – public static int MIN_PRIORITY --- with value 1 – public static int NORM_PRIORITY --- with value 5 – public static int MAX_PRIORITY --- with value 10 • Default priority of a thread is 5 (NORM_PRIORITY). • The JVM always picks the currently runnable thread with the highest priority. • A lower priority thread can run only when no higher-priority threads are running. • If all runnable threads have equal priorities, each is assigned an equal portion of the CPU time in a circular queue. This is called round-robin scheduling. • You can reset the priority using setPriority(int priority). • For example, suppose you insert the following code in TaskThreadDemo.java for thread3. thread3.setPriority(Thread.MAX_PRIORITY); • The thread for the print100 task will be finished first. 26
  • 27. Thread Scheduling in Java • An operating system’s thread scheduler determines which thread runs next. • Most operating systems use time slicing for threads of equal priority. • Thread scheduler in java is the part of the JVM that decides which thread should run. • There is no guarantee that which runnable thread will be chosen to run by the thread scheduler. • Only one thread at a time can run in a single process. • The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads. • Preemptive scheduling: the highest priority task executes until it enters a higher priority task comes into existence. when a thread of higher priority enters the running state, it preempts the current thread. • Time slicing: a task executes for a predefined slice of time and then reenters the pool of ready tasks. 27
  • 28. Thread Scheduling in Java • Starvation: Higher-priority threads can postpone (possible forever) the execution of lower-priority threads. • A thread may never get a chance to run if there is always a higher-priority thread running or a same-priority thread that never yields. • This situation is known as contention or starvation. • To avoid contention, the thread with higher priority must periodically invoke the sleep or yield method to give a thread with a lower or the same priority a chance to run. 28
  • 29. Java Thread Pool 29 • Thread pool can be used to execute tasks efficiently • Defining tasks and threads independently is convenient for a single task execution, but it is not efficient for a large number of tasks, because you have to create a thread for each task. • Starting a new thread for each task could limit throughput and cause poor performance. • A thread pool is ideal to manage the number of tasks executing concurrently. • A thread pool reuses previously created threads to execute current tasks and offers a solution to the problem of thread cycle overhead and resource thrashing. • Java provides the Executor interface for executing tasks in a thread pool and the ExecutorService interface for managing and controlling tasks.
  • 30. • ExecutorService is a sub-interface of Executor, as shown in Figure below. Shuts down the executor, but allows the tasks in the executor to complete. Once shutdown, it cannot accept new tasks. Shuts down the executor immediately even though there are unfinished threads in the pool. Returns a list of unfinished tasks. Returns true if the executor has been shutdown. Returns true if all tasks in the pool are terminated. ÂŤinterfaceÂť java.util.concurrent.Executor +execute(Runnable object): void Executes the runnable task. ÂŤinterfaceÂť java.util.concurrent.ExecutorService +shutdown(): void +shutdownNow(): List<Runnable> +isShutdown(): boolean +isTerminated(): boolean Java Thread Pool 30
  • 31. Creating Executors To create an Executor object, use the static methods in the Executors class as shown in Figure below. The newFixedThreadPool(int) method creates a fixed number of threads in a pool. If a thread completes executing a task, it can be reused to execute another task. If a thread terminates due to a failure prior to shutdown, a new thread will be created to replace it if all the threads in the pool are not idle and there are tasks waiting for execution. The newCachedThreadPool() method creates a new thread if all the threads in the pool are not idle and there are tasks waiting for execution. A thread in a cached pool will be terminated if it has not been used for 60 seconds. A cached pool is efficient for many short tasks. Creates a thread pool with a fixed number of threads executing concurrently. A thread may be reused to execute another task after its current task is finished. Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. java.util.concurrent.Executors +newFixedThreadPool(numberOfThreads: int): ExecutorService +newCachedThreadPool(): ExecutorService 31
  • 32. Example: Thread Pool import java.util.concurrent.*; public class ExecutorDemo { public static void main(String[] args) { // Create a fixed thread pool with maximum three threads ExecutorService executor = Executors.newFixedThreadPool(3); // Submit runnable tasks to the executor executor.execute( new PrintChar('a' , 100)); executor.execute(new PrintChar(‘b' , 100)); executor.execute(new PrintNum(100)); // Shut down the executor executor.shutdown(); } }  The following program shows how to rewrite the code in TaskThreadDemo.java using a thread pool.  Steps to be follow: 1. Create a task (Runnable object) to execute 2. Create Executor Pool using Executors 3. Pass tasks to Executor Pool 4. Shutdown the Executor Pool 32
  • 33. Thread Synchronization • Thread Synchronization is the capability to control the access of multiple threads to any shared resource. • Java Synchronization is better option where we want to allow only one thread to access the shared resource. • A shared resource may be corrupted if it is accessed simultaneously by multiple threads. • Example: two unsynchronized threads accessing the same bank account may cause conflict. Task 1 and Task 2 both add 1 to the same balance 33
  • 34. Example: Showing Resource Conflict • Objective: Write a program that demonstrates the problem of resource conflict. Suppose that you create and launch one hundred threads, each of which adds a penny to an account. Assume that the account is initially empty. 34
  • 35. import java.util.concurrent.*; public class AccountWithoutSync { private static Account account = new Account(); public static void main(String[] args) { ExecutorService executor = Executors.newCachedThreadPool(); for (int i = 0; i < 100; i++) { executor.execute(new AddAPennyTask()); } executor.shutdown(); while (!executor.isTerminated() ) { } System.out.println("What is balance? " + account.getBalance()); } private static class AddAPennyTask implements Runnable { public void run() { account.deposit(1); } } private static class Account { private int balance = 0; public int getBalance() { return balance; } public void deposit(int amount) { int newBalance = balance + amount; try { Thread.sleep(5); } catch (InterruptedException ex) { } balance = newBalance; } } } Example: Showing Resource Conflict 35
  • 36. Race Condition What, then, caused the error in the example? Here is a possible scenario: Step balance Task 1 Task 2 1 0 newBalance = balance + 1; 2 0 newBalance = balance + 1; 3 1 balance = newBalance; 4 1 balance = newBalance; ); • Effect: Task 1 did nothing (in Step 4 Task 2 overrides the result) • Problem: Task 1 and Task 2 are accessing a common resource in a way that causes conflict. • Known as a race condition in multithreaded programs. •A thread-safe class does not cause a race condition in the presence of multiple threads. The Account class is not thread-safe. 36
  • 37. The synchronized Keyword 37 • Problem: race conditions • Solution: give exclusive access to one thread at a time to code that manipulates a shared object. • To avoid race conditions, it is necessary to prevent more than one thread from simultaneously entering a certain part of the program, known as the critical region. • Synchronization keeps other threads waiting until the object is available. • The synchronized keyword synchronizes the method so that only one thread can access the method at a time. • The critical region in the previous code (AccountWithoutSync.java) is the entire deposit method. • One way to correct the problem in this code: make Account thread-safe by adding the synchronized keyword in deposit method as follows: public synchronized void deposit(double amount)
  • 38. Synchronizing Instance Methods and Static Methods 38 • A synchronized method acquires a lock before it executes. • A lock is a mechanism for exclusive use of a resource. • Instance method: the lock is on the object for which it was invoked. • Invoking a synchronized instance method of an object acquires a lock on the object. • Static method: the lock is on the class. • Invoking a synchronized static method of a class acquires a lock on the class. • If one thread invokes a synchronized instance method (respectively, static method) on an object, the lock of that object (respectively, class) is acquired, then the method is executed, and finally the lock is released. • Another thread invoking the same method of that object (respectively, class) is blocked until the lock is released.
  • 39. With the deposit method synchronized, the preceding scenario cannot happen. If Task 2 starts to enter the method, and Task 1 is already in the method, Task 2 is blocked until Task 1 finishes the method. Acquire a lock on the object account -char token +getToken +setToken +paintComponet +mouseClicked Execute the deposit method -char token +getToken +setToken +paintComponet +mouseClicked Release the lock -char token +getToken +setToken +paintComponet +mouseClicked Task 1 -char token +getToken +setToken +paintComponet +mouseClicked Acqurie a lock on the object account -char token +getToken +setToken +paintComponet +mouseClicked Execute the deposit method -char token +getToken +setToken +paintComponet Release the lock Task 2 -char token +getToken +setToken +paintComponet +mouseClicked Wait to acquire the lock -char token +getToken +setToken +paintComponet +mouseClicked 39 Synchronizing Instance Methods and Static Methods
  • 40. Synchronized Block • Synchronized block can be used to perform synchronization on any specific resource of the method. • Suppose you have 50 lines of code in your method, but you want to synchronize only 5 lines, you can use synchronized block. • Synchronized block is used to lock an object for any shared resource. • Scope of synchronized block is smaller than the method. synchronized (object reference expression) { statements; } • If the object is already locked by another thread, the thread is blocked until the lock is released. • When a lock is obtained on the object, the statements in the synchronized block are executed, and then the lock is released. • Synchronized statements enable you to synchronize part of the code in a method instead of the entire method. This increases concurrency. • You can make AccountWithoutSync.java thread-safe by placing the statement inside a synchronized block: synchronized (account) { account.deposit(1); } 40
  • 41. Inter-Thread Communication (Cooperation Among Threads) • Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each other. • Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed. It is implemented by following methods of Object class: • wait(): this method lets the thread wait until some condition occurs. • notify(): this method wakes up only one thread from a waiting queue. • notifyAll(): this method wakes up all waiting threads. • Use the wait(), notify(), and notifyAll() methods to facilitate communication among threads. • The wait(), notify(), and notifyAll() methods must be called in a synchronized method or a synchronized block on the calling object of these methods. Otherwise, an IllegalMonitorStateException would occur. • Conditions can be used for communication among threads. 41
  • 42. • Example: To demonstrate thread communications. • Suppose that you create and launch two tasks: 1. one that deposits into an account, and 2. one that withdraws from the same account. • The withdraw task has to wait if the amount to be withdrawn is more than the current balance. • Whenever new funds are deposited into the account, the deposit task notifies the withdraw thread to resume. • If the amount is still not enough for a withdrawal, the withdraw thread has to continue to wait for a new deposit. 42 Inter-Thread Communication (Cooperation Among Threads)
  • 43. while (balance < withdrawAmount) newDeposit.await(); Withdraw Task -char token +getToken +setToken +paintComponet +mouseClicked balance -= withdrawAmount -char token +getToken +setToken lock.unlock(); Deposit Task -char token +getToken +setToken +paintComponet +mouseClicked lock.lock(); -char token +getToken +setToken +paintComponet +mouseClicked newDeposit.signalAll(); balance += depositAmount -char token +getToken +setToken +paintComponet +mouseClicked lock.unlock(); -char token lock.lock(); -char token +getToken +setToken +paintComponet +mouseClicked 43 Inter-Thread Communication (Cooperation Among Threads) • To synchronize the operations, use a lock with a condition to synchronize operations: newDeposit • If the balance is less than the amount to be withdrawn, the withdraw task will wait for the newDeposit condition. • When the deposit task adds money to the account, the task signals the waiting withdraw task to try again. • Interaction between the two tasks:
  • 44. Semaphores Acquire a permit from a semaphore. Wait if the permit is not available. -char token +getToken +setToken +paintComponet +mouseClicked A thread accessing a shared resource -char token +getToken +setToken +paintComponet +mouseClicked Access the resource -char token +getToken +setToken +paintComponet Release the permit to the semaphore -char token semaphore.acquire(); A thread accessing a shared resource -char token +getToken +setToken +paintComponet +mouseClicked Access the resource -char token +getToken +setToken +paintComponet semaphore.release(); -char token 44 • Semaphores can be used to restrict the number of threads that access a shared resource. • A semaphore is an object that controls the access to a common resource. • Before accessing the resource, a thread must acquire a permit from the semaphore. • After finishing with the resource, the thread must return the permit back to the semaphore, as shown in Figure below.
  • 45. Creating Semaphores Creates a semaphore with the specified number of permits. The fairness policy is false. Creates a semaphore with the specified number of permits and the fairness policy. Acquires a permit from this semaphore. If no permit is available, the thread is blocked until one is available. Releases a permit back to the semaphore. java.util.concurrent.Semaphore +Semaphore(numberOfPermits: int) +Semaphore(numberOfPermits: int, fair: boolean) +acquire(): void +release(): void 45 • To create a semaphore, you have to specify the number of permits with an optional fairness policy, as shown in Figure below. • A task acquires a permit by invoking the semaphore’s acquire() method and releases the permit by invoking the semaphore’s release() method. • Once a permit is acquired, the total number of available permits in a semaphore is reduced by 1. • Once a permit is released, the total number of available permits in a semaphore is increased by 1.
  • 46. Deadlock 46 • Deadlock in java is a part of multithreading. Deadlock can occur in a situation when a thread is waiting for an object lock, that is acquired by another thread and second thread is waiting for an object lock that is acquired by first thread. Since, both threads are waiting for each other to release the lock, the condition is called deadlock. • Consider the scenario with two threads and two objects, as shown in Figure below. Thread 1 has acquired a lock on object1, and Thread 2 has acquired a lock on object2. Now Thread 1 is waiting for the lock on object2, and Thread 2 for the lock on object1. Each thread waits for the other to release the lock it needs, and until that happens, neither can continue to run.
  • 47. Preventing Deadlock 47 • Deadlocks can be easily avoided by using a proper resource ordering. • With this technique, assign an order on all the objects whose locks must be acquired and ensure that the locks are acquired in that order. • For the above example, suppose that the objects are ordered as object1 and object2. • Using the resource ordering technique, Thread 2 must acquire a lock on object1 first, then on object2. • Once Thread 1 acquires a lock on object1, Thread 2 has to wait for a lock on object1. Thus, Thread 1 will be able to acquire a lock on object2 and no deadlock will occur.