2. Objectives
On completion of this period, you would be able
to learn
• Deadlock problem
• Threads and deadlock
• Example Java program
• Avoiding deadlock
2
3. Recap
In the previous class, you have learnt
• Inter thread communication methods
• About wait() method
• About notify() method
• Producer-Consumer problem
• Solution to Producer–Consumer problem
3
4. Deadlock Problem
• A set of blocked processes each holding a resource
and waiting to acquire a resource held by another
process in the set
• Example
• System has 2 tape drives
• P1 and P2 each hold one tape drive and each needs another
one
• Example
• semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
4
5. Bridge Crossing Example
Fig. 39.1 Bridge crossing example for deadlock
• Each section of a bridge can be viewed as a
resource
• If two cars in each section of the bridge try to
cross the bridge at the same time, deadlock
occurs
• If a deadlock occurs, it can be resolved if one car
backs up (preempt resources and rollback)
• Several cars may have to be backed up if a
deadlock occurs
• Starvation is possible
5
6. Threads and Deadlock
• Occur when threads are mutually blocking each
other
• Not normally detected by Java Runtime
• Should be avoided by design
• Example :
Blocks object B
Blocks object A
Deadlock
Blocks object A
Blocks object B
Fig. 39.2. Illustration of Deadlock
6
7. Example Program
class A {
synchronized void method1(B b) {
String name = Thread.currentThread().getName();
System.out.println(name + " entered A. method1");
try {
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("A Interrupted");
}
System.out.println(name + " trying to call B.last()");
b.last();
} A call to another synchronized
synchronized void last() { method of B
System.out.println("Inside A.last");
}
}
7
8. Example Program
class B {
synchronized void method2(A a) {
String name = Thread.currentThread().getName();
System.out.println(name + " entered B. method2");
try {
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("B Interrupted");
}
System.out.println(name + " trying to call A.last()");
a.last();
A call to another synchronized
}
method of A
synchronized void last() {
System.out.println("Inside B.last");
}
}
8
9. Example Program
Contd . . .
class Deadlock implements Runnable {
A a = new A(); B b = new B(); Objects of A and B with
Deadlock() { methods method1() and
method2() are created
Thread.currentThread().setName("MainThread");
Thread t = new Thread(this, "RacingThread"); These methods pause
t.start(); briefly and call each other
through last() method
a. method1(b); // get lock on a in this thread.
System.out.println("Back in main thread"); As all these methods are
} synchronized,
DEADLOCK
public void run() {
occurs
b. method2(a); // get lock on b in other thread.
System.out.println("Back in other thread");
}
public static void main(String args[]) { new Deadlock(); }
9
}
10. Avoiding Deadlock
• Avoid having code in a restricted area that can
halt thread
• If multiple locks are required, request the locks in
the same order
• Example
Fig. 39.3 Avoiding Deadlock
10
11. Summary
• In this class, you have learnt
• The deadlock problem
• An illustration for deadlock
• Example program to show deadlock
• A technique to avoid deadlock
• In the next lesson we look at Thread properties
11
12. Quiz
1. A deadlock is caused by calling
A. A wait() method
B. Mutually blocking statements
C. A notify() method
D. A notifyAll() method
12
13. Quiz Contd..
2. While in deadlock, threads will be
A. Executing properly
B. Sleeping for specified time
C. Blocked ( not doing any work)
D. Waiting for a given time
13