Deadlock Java Multi-threading

Let’s study about Deadlock Java Multi-threading.

What is Deadlock in Multithreading?

Deadlock in multithreading is a situation where two or more threads are waiting for each other, blocked forever.

Example

Deadlock in Java Multithreading

Let’s under in a simple manner, assume there are two threads (as shown in the above diagram) Thread 1 and Thread 2 and two objects obj 1 and obj 2Thread 1 is already holds by an obj 1 and for further processing it needs to lock an obj 2. At the same time Thread 2 holds by an obj 2 and needs to lock an obj 1. In this condition kind of both threads waiting for each otherblocked forever.

Note: Deadlock condition is a complex condition which occurs only in case of multiple threads.

Coding

Below program create deadlock between two threads when the synchronized keyword causes the executing thread to block while waiting for the lock, in such case they will end up waiting forever.

package demo;

 

public class Test{

String str1 = “beyond”;

String str2 = “corner”;

 

Thread t1 = new Thread(“My Thread 1”){

public void run(){

while(true){

synchronized(str1){

synchronized(str2){

System.out.println(str1 + str2);

}

}

}

}

};

 

Thread t2 = new Thread(“My Thread 2”){

public void run(){

while(true){

synchronized(str1){

synchronized(str2){

System.out.println(str1 + str2);

}

}

}

}

};

 

public static void main(String[] args) {

Test T = new Test();

T.t1.start();

T.t2.start();

}

}

The above program will hang forever because of deadlock issue, so you can come out of the program by pressing CTRL+C.

Screenshot

Deadlock Solution Example

In this program let’s change the order of the lock then run the same program to recover from the deadlock situation.

package demo;

public class Test{
String str1 = “beyond”;
String str2 = “corner”;

Thread t1 = new Thread(“My Thread 1”){
public void run(){
while(true){
synchronized(str1){
synchronized(str2){
System.out.println(str1 + str2);
}
}
}
}
};

Thread t2 = new Thread(“My Thread 2”){
public void run(){
while(true){
synchronized(str2){
synchronized(str1){
System.out.println(str2 + str1);
}
}
}
}
};

public static void main(String[] args) {
Test T = new Test();
T.t1.start();
T.t2.start();
}
}

Screenshot

recover from the deadlock situation program

Note: Avoid Synchronization method and free from the Deadlock in Java Multithreading.

Why Deadlock occurs in Java Multithreading ?

For the following reasons Deadlock occurs in Multithreading,

* Usage of Nested Locks in java program.

* Due to Synchronization methods.

* Using unnecessary Locks in the java program.

* The nested synchronized blocks also leads to deadlock.

* Using thread join in the java program.

Note: Avoid the above reasons to free from the Deadlock.

Drawbacks of Deadlock in Java Multithreading

* Many library functions are not safe in java thread.

* If one thread crashes then the whole application will going to crashes.

* Threads executed serially so it increase the time complexity.

* Threads are completely dependents on Operating Systems.

* Global variables are shared between threads, accidental modification of shared variables causing great damage.

“That’s all about the Deadlock in Java Multithreading, it’s one of the main area where java programmer need to take care”

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *