Thread Deadlock in Java

Thread Deadlock

Deadlock describes condition where two or more threads are blocked forever, waiting for each other. In deadlock situation, both the threads keep waiting for each other to release the lock.

Example : Program to exhibit deadlock in Java

public class DeadlockDemo
{
      String s1 = "Window";
      String s2 = "Linux";
      Thread t1 = new Thread(" Thread 1")
      {
            public void run()
            {
                  while(true)
                  {
                        synchronized(s1)
                        {
                              synchronized(s2)
                              {
                                   System.out.println(s1 + s2);
                              }
                        }
                  }
            }
      };
Thread t2 = new Thread(" Thread 2")
{
      public void run()
      {
           while(true)
           {
                 synchronized(s2)
                 {
                        synchronized(s1)
                        {
                              System.out.println(s2 + s1);
                        }
                 }
           }
      }
};
      public static void main(String a[])
      {
             DeadlockDemo dd = new DeadlockDemo();
             dd.t1.start();
             dd.t2.start();
      }
}

Inter Thread Communication

Inter thread communication is used when an application has two or more threads that exchange same information. Inter thread communication helps in avoiding thread pooling.  

Three methods used for thread communication are:
  • wait ()
  • notify ()
  • notifyAll ()
1. wait (): It causes the current thread to wait until another thread invokes the notify ( ) or notifyAll( ) method.

For example:
public final void wait () throws InterruptedException,
public final void wait (long timeout) InterruptedException

2. notify () : Wakes up the single thread waiting on the object monitor.

Syntax:
public final void notify ()

3. notifyAll () : Wakes up all threads waiting on the object monitor.

Syntax:
public final void notifyAll()

Example : Sample program to implement inter thread communication

public class InterThreadDemo implements Runnable
{
     public void run ()
     {
           synchronized (InterThreadDemo.class)
           {
                System.out.println ("Waiting: " + this);
                try
                {
                     InterThreadDemo.class.wait ();
                }
                catch (InterruptedException ex)
                {
                     return;
                }
                System.out.println ("Notified: " + this);
           }
     }
     public static void main (String [] args) throws Exception
     {
           for (int i = 0; i < 10; i++)
           new Thread (new InterThreadDemo ()).start ();
           Thread.sleep (100);
           System.out.println ("notify () method ");
           synchronized (InterThreadDemo.class)
           {
                InterThreadDemo.class.notify ();
           }
           Thread.sleep (100);
           System.out.println ("notifyAll () method ");
           synchronized (InterThreadDemo.class)
           {
                InterThreadDemo.class.notifyAll ();
           }
     }
}


Output:
Waiting: InterThreadDemo@7ce229be
Waiting: InterThreadDemo@210ace8d
Waiting: InterThreadDemo@2608fb65
Waiting: InterThreadDemo@52d85409
Waiting: InterThreadDemo@7875455c
notify () method
Notified: InterThreadDemo@7ce229be
notifyAll () method
Notified: InterThreadDemo@7875455c
Notified: InterThreadDemo@52d85409
Notified: InterThreadDemo@2608fb65
Notified: InterThreadDemo@210ace8d