SlideShare a Scribd company logo
Deadlocks




            1
Objectives

On completion of this period, you would be able
to learn

  •   Deadlock problem
  •   Threads and deadlock
  •   Example Java program
  •   Avoiding deadlock




                                                  2
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
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
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
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
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
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
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
}
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
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
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
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
Frequently Asked Questions

1. Define deadlock
2. Explain deadlock with examples
3. How to avoid deadlock ?




                                      14

More Related Content

PPT
04 threads
ambar khetan
 
PDF
Cp7 rpc
bhaktinalawade
 
DOCX
Jist of Java
Nikunj Parekh
 
DOCX
JAVA CONCEPTS AND PRACTICES
Nikunj Parekh
 
PPTX
Java Thread
DeeptiJava
 
ODP
Java Concurrency
Carol McDonald
 
PDF
Java Concurrency Gotchas
Alex Miller
 
PPTX
The Java memory model made easy
Rafael Winterhalter
 
04 threads
ambar khetan
 
Jist of Java
Nikunj Parekh
 
JAVA CONCEPTS AND PRACTICES
Nikunj Parekh
 
Java Thread
DeeptiJava
 
Java Concurrency
Carol McDonald
 
Java Concurrency Gotchas
Alex Miller
 
The Java memory model made easy
Rafael Winterhalter
 

What's hot (20)

PDF
Java Concurrency Idioms
Alex Miller
 
PPTX
advanced java ppt
PreetiDixit22
 
PDF
Java Concurrency Gotchas
Alex Miller
 
PPTX
Николай Папирный Тема: "Java memory model для простых смертных"
Ciklum Minsk
 
PPT
ThreadProperties
myrajendra
 
PPT
Java concurrency
ducquoc_vn
 
PDF
Multithreading done right
Platonov Sergey
 
PDF
Multithreading in Java
Appsterdam Milan
 
ODP
Java memory model
Michał Warecki
 
PPT
Threads
RanjithaM32
 
PPT
Synchronization.37
myrajendra
 
KEY
Basic Packet Forwarding in NS2
Teerawat Issariyakul
 
PPT
Java
javeed_mhd
 
PPTX
Basics of Java Concurrency
kshanth2101
 
PPT
Core java concepts
javeed_mhd
 
PDF
Java ppt Gandhi Ravi ([email protected])
Gandhi Ravi
 
PDF
Ti1220 Lecture 2: Names, Bindings, and Scopes
Eelco Visser
 
PDF
Variables: names, bindings, type, scope
suthi
 
PDF
Java 8 - Stamped Lock
Haim Yadid
 
PPTX
Thread syncronization
priyabogra1
 
Java Concurrency Idioms
Alex Miller
 
advanced java ppt
PreetiDixit22
 
Java Concurrency Gotchas
Alex Miller
 
Николай Папирный Тема: "Java memory model для простых смертных"
Ciklum Minsk
 
ThreadProperties
myrajendra
 
Java concurrency
ducquoc_vn
 
Multithreading done right
Platonov Sergey
 
Multithreading in Java
Appsterdam Milan
 
Java memory model
Michał Warecki
 
Threads
RanjithaM32
 
Synchronization.37
myrajendra
 
Basic Packet Forwarding in NS2
Teerawat Issariyakul
 
Basics of Java Concurrency
kshanth2101
 
Core java concepts
javeed_mhd
 
Java ppt Gandhi Ravi ([email protected])
Gandhi Ravi
 
Ti1220 Lecture 2: Names, Bindings, and Scopes
Eelco Visser
 
Variables: names, bindings, type, scope
suthi
 
Java 8 - Stamped Lock
Haim Yadid
 
Thread syncronization
priyabogra1
 
Ad

Similar to Dead locks9cm604.39 (20)

PPTX
Java class 6
Edureka!
 
PPT
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
PDF
Concurrency: Best Practices
IndicThreads
 
PPTX
JEEConf 2017 - How to find deadlock not getting into it
Nikita Koval
 
PPT
Threadlifecycle.36
myrajendra
 
PPTX
Concurrency
Ankur Maheshwari
 
PDF
Java Concurrency by Example
Ganesh Samarthyam
 
PDF
Java Concurrency by Example
CodeOps Technologies LLP
 
PPTX
Low-level concurrency (reinvent vehicle)
Olena Syrota
 
PDF
Java synchronizers
ts_v_murthy
 
PPTX
Concurrency with java
Luis Goldster
 
PPTX
Concurrency with java
Hoang Nguyen
 
PPTX
Concurrency with java
James Wong
 
PPTX
Concurrency with java
Young Alista
 
PPTX
Concurrency with java
Harry Potter
 
PPTX
Concurrency with java
Tony Nguyen
 
PPTX
Concurrency with java
Fraboni Ec
 
PPT
Java Multithreading and Concurrency
Rajesh Ananda Kumar
 
PDF
TMPA-2017: Dl-Check: Dynamic Potential Deadlock Detection Tool for Java Programs
Iosif Itkin
 
PPTX
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Java class 6
Edureka!
 
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
Concurrency: Best Practices
IndicThreads
 
JEEConf 2017 - How to find deadlock not getting into it
Nikita Koval
 
Threadlifecycle.36
myrajendra
 
Concurrency
Ankur Maheshwari
 
Java Concurrency by Example
Ganesh Samarthyam
 
Java Concurrency by Example
CodeOps Technologies LLP
 
Low-level concurrency (reinvent vehicle)
Olena Syrota
 
Java synchronizers
ts_v_murthy
 
Concurrency with java
Luis Goldster
 
Concurrency with java
Hoang Nguyen
 
Concurrency with java
James Wong
 
Concurrency with java
Young Alista
 
Concurrency with java
Harry Potter
 
Concurrency with java
Tony Nguyen
 
Concurrency with java
Fraboni Ec
 
Java Multithreading and Concurrency
Rajesh Ananda Kumar
 
TMPA-2017: Dl-Check: Dynamic Potential Deadlock Detection Tool for Java Programs
Iosif Itkin
 
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Ad

More from myrajendra (20)

PPT
Fundamentals
myrajendra
 
PPT
Data type
myrajendra
 
PPTX
Hibernate example1
myrajendra
 
PPTX
Jdbc workflow
myrajendra
 
PPTX
2 jdbc drivers
myrajendra
 
PPTX
3 jdbc api
myrajendra
 
PPTX
4 jdbc step1
myrajendra
 
PPTX
Dao example
myrajendra
 
PPTX
Sessionex1
myrajendra
 
PPTX
Internal
myrajendra
 
PPTX
3. elements
myrajendra
 
PPTX
2. attributes
myrajendra
 
PPTX
1 introduction to html
myrajendra
 
PPTX
Headings
myrajendra
 
PPTX
Forms
myrajendra
 
PPT
Css
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Views
myrajendra
 
PPT
Starting jdbc
myrajendra
 
Fundamentals
myrajendra
 
Data type
myrajendra
 
Hibernate example1
myrajendra
 
Jdbc workflow
myrajendra
 
2 jdbc drivers
myrajendra
 
3 jdbc api
myrajendra
 
4 jdbc step1
myrajendra
 
Dao example
myrajendra
 
Sessionex1
myrajendra
 
Internal
myrajendra
 
3. elements
myrajendra
 
2. attributes
myrajendra
 
1 introduction to html
myrajendra
 
Headings
myrajendra
 
Forms
myrajendra
 
Views
myrajendra
 
Views
myrajendra
 
Views
myrajendra
 
Starting jdbc
myrajendra
 

Dead locks9cm604.39

  • 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
  • 14. Frequently Asked Questions 1. Define deadlock 2. Explain deadlock with examples 3. How to avoid deadlock ? 14