Friday 12 April 2013

What is deadlock in java? How can we detect and how to avoid deadlock in java?

Deadlock



Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
 
When two or more threads waiting for each other to release lock and then stucked for infinite time, this condition is known as deadlock. For detection of deadlock, we can use jconsole, which will tell exactly which thread is getting locked due to which object. For avoiding deadlock we need to provide an ordered access of resources to threads.


A sample program to create deadlock

package com.gaurav.multithreads;

/**
 * Here I am demonstration of how NOT to write multithreaded programs, which will result a deadlock.
 * It is a program that purposely causes deadlock between two threads that
 * are both trying to acquire locks for the same two resources.
 * To avoid this sort of deadlock when locking multiple resources, all threads
 * should always acquire their locks in the same order.
 **/
public class Deadlock {
 
    public static void main(String[] args) {
   
        /** These are the two resource objects we'll try to get locks for */
    final Object FIRST_RESOURCE = "first_resource";
    final Object SECOND_RESOURCE = "second_resource";
   
    /** Here's the first thread.  It tries to lock FIRST_RESOURCE then SECOND_RESOURCE */
    Thread t1 = new Thread() {
      public void run() {
       
          /** Lock FIRST_RESOURCE */
        synchronized(FIRST_RESOURCE) {
          System.out.println("Thread 1: locked FIRST_RESOURCE");

          /** Pause for a moment. 
           Here we are giving a chance to the other thread to
           run.  Threads and deadlock are asynchronous, but intentionally we're
           trying to create a deadlock here...*/
          try {
              Thread.sleep(100);
              }
          catch (InterruptedException ie) {
              System.out.println(ie.getMessage());
          }
         
          /** Now wait till we can get a lock on SECOND_RESOURCE */
          synchronized(SECOND_RESOURCE) {
            System.out.println("Thread 1: locked SECOND_RESOURCE");
          }
        }
      }
    };
   
    /** Here's the second thread.  It tries to lock SECOND_RESOURCE then FIRST_RESOURCE */
    Thread t2 = new Thread() {
      public void run() {
       
          /** This thread locks SECOND_RESOURCE now */
        synchronized(SECOND_RESOURCE) {
          System.out.println("Thread 2: locked SECOND_RESOURCE");

          /** Then it pauses  for the same reason as the first thread does */
          try {
              Thread.sleep(100);
              }
          catch (InterruptedException ie) {
              System.out.println(ie.getMessage());
          }

          /** Then it tries to lock FIRST_RESOURCE.  But wait!  Thread 1 locked
           FIRST_RESOURCE, and won't release it till it gets a lock on
           SECOND_RESOURCE.  This thread holds the lock on SECOND_RESOURCE, and won't
           release it, till it gets FIRST_RESOURCE.  Now no threads can run and the programs halts.*/
           synchronized(FIRST_RESOURCE) {
            System.out.println("Thread 2: locked FIRST_RESOURCE");
          }
        }
      }
    };
   
    /** Start both the threads. Now, deadlock will occur, and the program will never exit. */
    t1.start();
    t2.start();
  }
}


Result:-







No comments:

Post a Comment