Deadlocks            1
ObjectivesOn completion of this period, you would be ableto learn  •   Deadlock problem  •   Threads and deadlock  •   Exa...
RecapIn the previous class, you have learnt•   Inter thread communication methods•   About wait() method•   About notify()...
Deadlock Problem• A set of blocked processes each holding a resource  and waiting to acquire a resource held by another  p...
Bridge Crossing Example         Fig. 39.1 Bridge crossing example for deadlock• Each section of a bridge can be viewed as ...
Threads and Deadlock• Occur when threads are mutually blocking each  other• Not normally detected by Java Runtime• Should ...
Example Programclass A {    synchronized void method1(B b) {         String name = Thread.currentThread().getName();      ...
Example Program     class B {      synchronized void method2(A a) {            String name = Thread.currentThread().getNam...
Example Program                                                        Contd . . .class Deadlock implements Runnable {  A ...
Avoiding Deadlock• Avoid having code in a restricted area that can  halt thread• If multiple locks are required, request t...
Summary• In this class, you have learnt   •   The deadlock problem   •   An illustration for deadlock   •   Example progra...
Quiz1. A deadlock is caused by calling  A.   A wait() method  B.   Mutually blocking statements  C.   A notify() method  D...
Quiz Contd..2. While in deadlock, threads will be  A.   Executing properly  B.   Sleeping for specified time  C.   Blocked...
Frequently Asked Questions1. Define deadlock2. Explain deadlock with examples3. How to avoid deadlock ?                   ...
Upcoming SlideShare
Loading in...5
×

Dead locks9cm604.39

107

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
107
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dead locks9cm604.39

  1. 1. Deadlocks 1
  2. 2. ObjectivesOn completion of this period, you would be ableto learn • Deadlock problem • Threads and deadlock • Example Java program • Avoiding deadlock 2
  3. 3. RecapIn 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. 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. 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. 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 BBlocks object A Deadlock Blocks object ABlocks object B Fig. 39.2. Illustration of Deadlock 6
  7. 7. Example Programclass 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. 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. 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. 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. 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. 12. Quiz1. A deadlock is caused by calling A. A wait() method B. Mutually blocking statements C. A notify() method D. A notifyAll() method 12
  13. 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
  14. 14. Frequently Asked Questions1. Define deadlock2. Explain deadlock with examples3. How to avoid deadlock ? 14
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×