Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 27 November 2016

What is Deadlock In Java. And How to Avoid It ?

Previously we have discussed about


DeadLock

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
Means when a thread is waiting for an object lock that is acquired by another thread and second thread is waiting for an object lock that is acquired by the first thread. So both the threads are waiting for each other to release a lock ,this situation or condition is called a Deadlock.

Example Showing Deadlock:
if thread1  and thread1 both will be called by two or many threads ,there is a good chance of deadlock because if thread 1 acquires lock on Sting object  while executing thread1 and thread 2 acquires lock on Integer object while executing thread2 both will be waiting for each other to release lock on Integer and String to proceed further which will never happen.

public class DeadLockDemo {
public static void main(String[] args) {
Thread thread1 = new Thread(){
public void run(){
synchronized(String.class){
System.out.println("Thread 1 : Aquired lock on String.class object");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(Integer.class){
System.out.println("Thread 1 : Aquired lock on Integer.class object");
}
}
}
};
Thread thread2 = new Thread(){
public void run(){
synchronized (Integer.class) {
System.out.println("Thread 2 : Aquired lock on Integer.class object");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (String.class) {
System.out.println("Thread 2 : Aquired lock on String.class object");
}
}
}
};
thread1.start();
thread2.start();
}
}
Program Output:
Thread 1 : Aquired lock on String.class object
Thread 2 : Aquired lock on Integer.class object



How to avoid deadlocks:

The best way to prevent deadlock is to avoid acquiring more than one lock at a time. However If it is not possible then you need to ensure that resources must be defined in some order.

Example Avoiding Deadlock:
Now there would not be any deadlock because both Threads are accessing lock on  Integer and String class literal in same order. So, if thread1 acquires lock on Integer object , thread2 will not proceed until thread1 releases Integer lock, same way thread1 will not be  blocked even if thread2 holds String lock because now thread2 will not expect thread1 to release Integer lock to proceed further.

public class DeadLockAvoid {
public static void main(String[] args) {
Thread thread1 = new Thread(){
public void run(){
synchronized(String.class){
System.out.println("Thread 1 : Aquired lock on String.class object");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(Integer.class){
System.out.println("Thread 1 : Aquired lock on Integer.class object");
}
}
}
};
Thread thread2 = new Thread(){
public void run(){
synchronized(String.class){
System.out.println("Thread 2 : Aquired lock on String.class object");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(Integer.class){
System.out.println("Thread 2 : Aquired lock on Integer.class object");
}
}
}
};
thread1.start();
thread2.start();
}
}
Program Output:
Thread 1 : Aquired lock on String.class object
Thread 1 : Aquired lock on Integer.class object
Thread 2 : Aquired lock on String.class object
Thread 2 : Aquired lock on Integer.class object



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment