group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
262 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
08 Feb 2019

What is Deadlock in java?

Deadlock in java is a condition which occurs when two or more threads get blocked waiting for each other for an infinite period of time to release the resources(Locks) they hold. Deadlock is the common problem in multi threaded programming which can completely stops the execution of an application. So, extra care need to be taken while writing the multi threaded programs so that deadlock never occurs.

AdhikariHarika Reshma
AdhikariHarika Reshma
Computer Science
2mos ago

Deadlock in java is a part of multithreading.Deadlock can occur in a situation 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 first thread.It describes a situation where two or more thread are blocked forever,waiting for each other.Since both the threads are waiting for each other to release the lock the condition is called deadlock.

Example:

public class Test{

public static void main(String args[]){

final String r1="Rakesh";

final String r2="Sunny";

//t1 tries to lock r1 then r2

Thread t1=new Thread();

public void run(){

synchronized (r1){

System.out.println("Thread 1: locked resource1");

try{

Thread.sleep(100);}

catch(Exception e) {}

synchronized(r2){

System.out.println("Thread 1:locked resource 2");

}

};

//t2 tries to lock r2 then r1

Thread t2=new Thread(){

public void run(){

synchronized(r2){

System.out.println("Thread 2:locked resource2");

try{

Thread.sleep(100);}

catch(Exception e) {}

synchronized(r1){

System.out.println("Thread 2:locked resource 1");

}

}

}

};

t1.start();

t2.start();

}

}

Archana Priyadarshini
Archana Priyadarshini
Computer Science
2mos ago

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in different order.

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.

If threads are waiting for each other to finish, then the condition is known as Deadlock.

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

Deadlock condition can break our code at run time and can destroy business logic.We should avoid this condition as much as we can.

Shruthi Pillai
Shruthi Pillai
Information Technology
2mos ago

When two or more threads are waiting for each other to release the resource they need (lock) and get stuck for infinite time, the situation is called deadlock. It will only happen in the case of multi tasking and multi threading.

The real reason for deadlock is not multiple threads, but it is the way that they are requesting a lock. If you provide an ordered access, then the problem will be resolved.