Showing posts with label Deadlocks. Show all posts
Showing posts with label Deadlocks. Show all posts

Sunday, 16 August 2020

Deadlock in Java MultiThreading

Creating a deadlock can be pretty easy, consider two strings(str1, str2) accessed by two different threads say T1, T2.

  1. Thread T1 & T2 are mutually exclusive.
  2. T1 acquires thread on String  str1 & goes to sleep.
  3. Thread T2 acquires the thread on str2 & goes to sleep.
  4. After the sleep interval of T1 gets over T1 tries to acquire the lock on str2, but sees that str2 is locked by T2. So it goes into blocking state
  5. Thread T2 also tries to acquire the lock on str1, but sees that str1 is locked by T1 & therefore goes into blocking state.
  6. The above point#4 & point #5 goes forever & none of the threads can proceed.
  7. Such a scenario is a deadlock.

 class DeadlockExample  
 {  
      public static void main (String[] args) throws java.lang.Exception  
      {  
           String s1 = "abc";  
           String s2 = "abcd";  
           Thread t1 = new Thread(new Thread1(s1,s2));  
           Thread t2 = new Thread(new Thread2(s1,s2));  
           t1.start();  
           t2.start();  
      }  
 }  
 class Thread1 implements Runnable{  
      private String str1;  
      private String str2;  
      public Thread1(String str1, String str2){  
           this.str1 = str1;  
           this.str2 = str2;  
      }  
      public void run(){  
           synchronized(str1){  
                try{Thread.sleep(1000);}catch(Exception e){}  
                synchronized(str2){  
                     System.out.println("Thread1");  
                }  
           }  
      }  
 }  
 class Thread2 implements Runnable{  
      private String str1;  
      private String str2;  
      public Thread2(String str1, String str2){  
           this.str1 = str1;  
           this.str2 = str2;  
      }  
      public void run(){  
           synchronized(str2){  
                try{Thread.sleep(1000);}catch(Exception e){}  
                synchronized(str1){  
                     System.out.println("Thread2");  
                }  
           }  
      }  
 }  

Thursday, 7 April 2016

Deadlocks


What is a deadlock?

Deadlocks are the most common scenarios occuring in a multi-threaded environment. In computing/technical terms a deadlock can be a scenario where there are two or more parties waiting for a resource but neither of them can proceed.

How can a deadlock happen?

A deadlock situation can arise if and only if the following four conditions occur at the same time in the system -
  1. Mutual Exclusion: At least one resource is held in a non-sharable mode that is only one process at a time can use the resource. If another process requests that resource, the requesting process must be delayed until the resource has been released.
  2. Hold and Wait:There must exist a process that is holding at least one resource and is waiting to acquire additional resources that are currently being held by other processes. 
  3. No Preemption: Resouces cannot be preempted; that is, a resource can only be released voluntarily by the process holding it, after the process has completed its task. 
  4. Circular Wait: There must exist a set {p0, p1,.....pn} of waiting processes such that p0 is waiting for a resource which is held by p1, p1 is waiting for a resource which is held by p2,..., pn-1 is waiting for a resource which is held by pn and pn is waiting for a resource which is held by p0
Ways to prevent a deadlock - 
There can be many ways to prevent a deadlock -
  1. Semaphore.
  2. Monitors.
  3. Mutex variable.