Sunday, 21 July 2019

Deadlock in Multithreading

  • If two threads are waiting for each other forever such type of situation (infinite waiting) is called deadlock.
  • Synchronized keyword is the only reason for deadlock situation. Hence while using Synchronized keyword we have to take special care.
  • There are no resolution techniques for deadlock but several prevention techniques are available.
Example:

class A
{
              public synchronized void foo(B b)
              {
                             System.out.println("Thread1 starts execution of foo() method");
                             try
                             {
                                           Thread.sleep(2000);
                             }
                             catch (InterruptedException e)
                             {}
                             System.out.println("Thread1 trying to call b.last()");
                             b.last();
              }
              public synchronized void last()
              {
                             System.out.println("inside A, this is last()method");
              }
}

class B
{
              public synchronized void bar(A a)
              {
                             System.out.println("Thread2 starts execution of bar() method");
                             try
                             {
                                           Thread.sleep(2000);
                             }
                             catch (InterruptedException e)
                             {}
                             System.out.println("Thread2 trying to call a.last()"); a.last();
              }
              public synchronized void last()
              {
                             System.out.println("inside B, this is last() method");
              }
}

class DeadLock implements Runnable
{
              A a=new A(); B b=new B();
              DeadLock()
              {
                             Thread t=new Thread(this);
                             t.start(); 
                             a.foo(b);//main thread
              }
              public void run()
              {
                             b.bar(a);//child thread
              }
              public static void main(String[] args)
              {
                             new DeadLock();//main thread
              }
}

Output:
Thread1 starts execution of foo() method
Thread2 starts execution of bar() method
Thread2 trying to call a.last()
Thread1 trying to call b.last()
//here cursor always waiting.

Note: If we remove at least one synchronized keyword then we won't get DeadLock. Hence synchronized keyword in the only reason for DeadLock due to this while using synchronized keyword we have to handling carefully.

Deadlock vs Starvation:
  • Long waiting of a Thread where waiting never ends is called deadlock.
  • Whereas long waiting of a Thread where waiting ends at certain point is called starvation.
  • For example, low priority Thread has to wait until completing all high priority Threads. It may be long waiting but ends at certain point, which is nothing but starvation.

Thanks..!!

No comments:

Post a Comment