DEAD LOCK

Dead Lock


  1. Here thread 1 call test1() method by using s1 object lock and thread 0 call test2() method by using s2 object lock. From test1() method thread1 is calling test2() method by using s2 which is already locked by thread1

  2. From test2() method thread 0 is calling test1() by using s1 lock which is already locked by thread0.

  3. So thread0 is waiting for thread1 completion and thread1 is waiting for thread0 completion

  4. Means both threads went to the waiting state. This state is called as “dead lock”


How to find out deadlock threads


  • isAlive():

    It is one of the important methods inside the Thread class. It will return one Boolean value i.e. true or false. If current thread is active mode it will return true else false.

    Syntax:

      While(t1.isAlive())
      {
       System.out.println(t2.isAlive());
      }


  • join():

    • We use join() method which is not advisable. If we use join() when the thread is in deadlock then main thread also in dead lock state. So it is not advisable.

    • The join() method of a Thread instance can be used to "join" the start of a thread's execution to the end of another thread's execution so that a thread will not start running until another thread has ended. If join() is called on a Thread instance, the currently running thread will block until the Thread instance has finished executing.

    • We can illustrate this with an example. The RunnableJob class implements Runnable. Its run() method displays the current thread's name and the time at which the run() method is executed. It then sleeps for 1 second.

    • java Thread join method can be used to pause the current thread execution until unless the specified thread is dead. There are three overloaded join functions.

    • public final void join(): This method puts the current thread on wait until the thread on which it’s called is dead. If the thread is interrupted, it throws InterruptedException.

    • public final synchronized void join(long millis): This method is used to wait for the thread on which it’s called to be dead or wait for specified milliseconds. Since thread execution depends on OS implementation, it doesn’t guarantee that the current thread will wait only for given time.

    • public final synchronized void join(long millis, int nanos): This method is used to wait for thread to die for given milliseconds plus nanoseconds..

Example 1:


        
      class MySdjJoin extends Thread
      {
         publicvoid run()
         {
            for(int i=0;i<10;i++)
            {
               try
               {
                  Thread.sleep(10);
               }
               catch(InterruptedException ex)
               {
                  ex.printStackTrace();
               }
               System.out.println(getName()+" : "+i);
            }
         }
      }
      public class MyJoinManager
      {
         public static void main(String[] args)
         {
            MySdjJoin my  = new MySdjJoin();
            MySdjJoin my1  = new MySdjJoin();
            MySdjJoin my2  = new MySdjJoin();
            try
            {
               Thread.currentThread().join(10);
               my.start();
               my.join();
               my1.start();
               my1.join();
               my2.start();
               my2.join();
            }
            catch(InterruptedException ex)
            {
               ex.printStackTrace();
            }
            System.out.println("Main Thread");
         }
      }