Friday, November 20, 2015

DeadLock in java Threads with example

In this article we will understand and learn about DeadLock in java and also solve how to avoid deadlock in java. This is frequently asked technical interview question.As a java developer must have knowledge on this topic.
DeadLock:

DeadLock describes a situation where two or more threads blocked forever,waiting for each other.

  • When a thread has locked an object and waiting for another object to be released by another Thread. And other Thread is also waiting for the first thread to release the first object,both the threads will continue waiting forever.This is called "Thread DeadLock".
  • Thread DeadLock is a drawback in a program.
  • The Programmer should take care to avoid any such deadlock in programs
  • Even if we Synchronize  the threads, there is a possibility of other problems like 'deadlock'

Let us understand this with  Example:

A Java multithreaded program may suffer from the deadlock condition because the
synchronized keyword causes the executing thread to block while waiting for the lock, or monitor,associated with the specified object. 

 Example:

public class TestThread {
public static Object Lock1 = new Object();
public static Object Lock2 = new Object();
public static void main(String args[]) {
ThreadDemo1 T1 = new ThreadDemo1();
ThreadDem o2 T2 = new ThreadDem o2();
T1.start();
T2.start();
}
private static class ThreadDem o1 extends Thread {
public void run() {
synchronized (Lock1) {
System .out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System .out.println("Thread 1: Waiting for lock 2...");
synchronized (Lock2) {
System .out.println("Thread 1: Holding lock 1 & 2...");
}
}
}
}
private static class ThreadDem o2 extends Thread {
public void run() {
synchronized (Lock2) {
System .out.println("Thread 2: Holding lock 2...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System .out.println("Thread 2: Waiting for lock 1...");
synchronized (Lock1) {
System .out.println("Thread 2: Holding lock 1 & 2...");
}
}
}
}

When you compile and execute the above program, you will  find a dead lock situation and below will be the  Result

output:

Thread 1: Holding lock 1...
Thread 2: Holding lock 2...
Thread 1: wating for lock 2...
Thread 2: Waiting for lock 1...

Note : The above program will waiting for each other object forever to release  lock. So you can come out of this program by pressing CTRL-C.

Avoid DeadLock  situation Example:

Let us change the order of lock and runs the preceding program.

public class TestThread {
public static Object Lock1 = new Object();
public static Object Lock2 = new Object();
public static void main(String args[]) {
ThreadDem o1 T1 = new ThreadDem o1();
ThreadDem o2 T2 = new ThreadDem o2();
T1.start();
T2.start();
}
private static class ThreadDemo1 extends Thread {
public void run() {
synchronized (Lock1) {
System .out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System .out.println("Thread 1: Waiting for lock 2...");
synchronized (Lock2) {
System .out.println("Thread 1: Holding lock 1 & 2...");
}
}
}
}
private static class ThreadDemo2 extends Thread {
public void run() {
synchronized (Lock1) {
System .out.println("Thread 2: Holding lock 1...");
try
Thread.sleep(10);
catch (InterruptedException e) {}
System .out.println("Thread 2: Waiting for lock 2...");
synchronized (Lock2) {
System .out.println("Thread 2: Holding lock 1 & 2...");
}
}
}
}
}

Note: Now just changing the order of the lock that prevent the program is going deadlock situation.

Output:

Thread 1: Holding lock 1...
Thread 1: Waiting for lock 2...
Thread 1: Holding lock 1 & 2...
Thread 2: Holding lock 2...
Thread 2: Waiting for lock 2...
Thread 2: Holding lock 1 & 2...




No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...