Interview Questions

What is deadlock in multithreading? Write a program to form DeadLock in multi threading and also how to solve DeadLock situation. What measures you should take to avoid deadlock?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

What is deadlock in multithreading? Write a program to form DeadLock in multi threading and also how to solve DeadLock situation. What measures you should take to avoid deadlock?

This is very important question from interview perspective. But, what makes this question important is it checks interviewees capability of creating and detecting deadlock. If you can write a code to form deadlock, than I am sure you must be well capable in solving that deadlock as well. If not, later on this post we will learn how to solve deadlock as well.

First question comes to mind is, what is deadlock in multi threading program? Deadlock is a situation where two threads are waiting for each other to release lock holded by them on resources.

But how deadlock could be formed :
Thread-1 acquires lock on String.class and then calls sleep() method which gives Thread-2 the chance to execute immediately after Thread-1 has acquired lock on String.class and Thread-2 acquires lock on Object.class then calls sleep() method and now it waits for Thread-1 to release lock on String.class.

Conclusion:
Now, Thread-1 is waiting for Thread-2 to release lock on Object.class and Thread-2 is waiting for Thread-1 to release lock on String.class and deadlock is formed.

Code called by Thread-1
public void run() {
synchronized (String.class) {
Thread.sleep(100);
synchronized (Object.class) {
}
}
}


Code called by Thread-2
public void run() {
synchronized (Object.class) {
Thread.sleep(100);
synchronized (String.class) {
}
}
}


Here comes the important part, how above formed deadlock could be solved :
Thread-1 acquires lock on String.class and then calls sleep() method which gives Thread-2 the chance to execute immediately after Thread-1 has acquired lock on String.class and Thread-2 tries to acquire lock on String.class but lock is holded by Thread-1. Meanwhile, Thread-1 completes successfully. As Thread-1 has completed successfully it releases lock on String.class, Thread-2 can now acquire lock on String.class and complete successfully without any deadlock formation.

Conclusion: No deadlock is formed.

Code called by Thread-1
public void run() {
synchronized (String.class) {
Thread.sleep(100);
synchronized (Object.class) {
}
}
}



Code called by Thread-2

public void run() {
synchronized (String.class) {
Thread.sleep(100);
synchronized (Object.class) {
}
}
}


Few important measures to avoid Deadlock :


1. Lock specific member variables of class rather than locking whole class: We must try to lock specific member variables of class rather than locking whole class.

2. Use join() method: If possible try to use join() method, although it may refrain us from taking full advantage of multithreading environment because threads will start and end sequentially, but it can be handy in avoiding deadlocks.

3. If possible try avoid using nested synchronization blocks.

(Continued on next question...)

Other Interview Questions