Multithreading has the concept of Deadlock. The first thread waits for a lock, and a second thread occupies that lock. The second thread is waiting for an object lock that is occupied by a previous(first) thread. It leads to deadlock condition because both threads are waiting for each other to release the lock.

Deadlock specifies a situation when two or more threads are waiting for each other (blocked to each other) to release their locks. The deadlock happens when the synchronized keyword makes the executing thread to block and wait for the monitor or block, combined with the specified object.

Deadlock occurs when a set of processes or threads are blocked because each process or thread is acquiring a resource and waiting for another resource held by some other process or thread.

In the example, when two cars are coming toward each other on the same track as they are in front of each other, none of them can move. The same situation happens in operating systems; there are two or more processes that have the resources and wait for the resources acquired by other processes. For example, Process1 is holding Resource1 and waiting for resource2, which is acquired by process2, and process2 is waiting for resource1 is explained in the below diagram: 

Deadlock in Java

There are the following four conditions hold simultaneously for the deadlock:

Mutual Exclusion: One or more than one resource is non-sharable and uses only one process at a time.

Hold and Wait: Atleast one resource is held by a process and waits for other resources.

No pre-emption: Unless the process releases the resource, it cannot be taken from a process.

Circular wait: In circular form, a set of processes are waiting for each other.

Avoid Dead Lock condition:

We can avoid deadlock conditions by knowing the possibilities.

Avoid nested Locks:

When we give locks to multiple threads, it will go for deadlock conditions. If we already have given a lock to one, avoid giving lock to multiple threads.

Avoid Unnecessary Locks:

The lock has been given to only those threads which require it. Giving lock on unnecessarily can lead to a deadlock.

Example1: Deadlock condition:

Output:

Deadlock in Java2

Explanation:

1. There are two threads Thread1 and Thread2 in the above program.

2. Thread2 holding the lock (named LockNew2) and enters into its synchronized method.

3. Thread1 holding the lock (named LockNew1) and enters into its synchronized method.

4.  As the Thread1 wait for the LockNew2 (lock) which is acquired by the Thread2.

5.  As the Thread2 wait for the LockNew1(lock) which is acquired by the Thread1.

6. The program is hanged forever because none of the threads in position to proceed and waiting for each other to release the lock.

7. So just by change in the locks order to prevent the program in going into a deadlock situation.

Example: Solution of the deadlock situation.

Output:

Deadlock in Java3

Explanation:

1.There are two threads Thread1 and Thread2 in the program.

2. Thread1 is having the lock (LockNew1) and it start their work then the Thread1 acquires the (LockNew2) the another lock for entering into the another synchronized block into the same run() method.
3. After complete the task of the Thread1 then Thread1 leaves both the locks.

4. After the both locks are released by the Thread1, then the Thread2 holds the LockNew1 lock and enters into the first synchronized block then the after holding the LockNew2 lock it enters into the second synchronized block and after completing the task or run() method Thread2 leaves both the locks.

5. Both the locks are then available for the other thread.

6. There is no conflict among the threads for the locks so there is no deadlock situation occurs.

Features of the Deadlock:

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

2. It is a complex condition that occurs in the case of multiple threads.

3. A deadlock condition can destroy business logic can break our code at run time.

Pin It on Pinterest

Share This