1. Overview
While multi-threading helps in improving an application's performance, it also comes with some problems. In this tutorial, we'll look into two such problems, deadlock and livelock, with the help of Java examples.
2. Deadlock
2.1. What Is Deadlock?
A deadlock occurs when two or more threads wait forever for a lock or resource held by another of the threads. Consequently, an application may stall or fail as the deadlocked threads cannot progress.
The classic dining philosophers problem nicely demonstrates the synchronization issues in a multi-threaded environment and is often used as an example of deadlock.
2.2. Deadlock Example
First, let's take a look into a simple Java example to understand deadlock.
In this example, we'll create two threads, T1 and T2. Thread T1 calls operation1, and thread T2 calls operations.
To complete their operations, thread T1 needs to acquire lock1 first and then lock2, whereas thread T2 needs to acquire lock2 first and then lock1. So, basically, both the threads are trying to acquire the locks in the opposite order.
Now, let's write the DeadlockExample class:
public class DeadlockExample { private Lock lock1 = new ReentrantLock(true); private Lock lock2 = new ReentrantLock(true); public static void main(String[] args) { DeadlockExample deadlock = new DeadlockExample(); new Thread(deadlock::operation1, "T1").start(); new Thread(deadlock::operation2, "T2").start(); } public void operation1() { lock1.lock(); print("lock1 acquired, waiting to acquire lock2."); sleep(50); lock2.lock(); print("lock2 acquired"); print("executing first operation."); lock2.unlock(); lock1.unlock(); } public void operation2() { lock2.lock(); print("lock2 acquired, waiting to acquire lock1."); sleep(50); lock1.lock(); print("lock1 acquired"); print("executing second operation."); lock1.unlock(); lock2.unlock(); } // helper methods }
Let's now run this deadlock example and notice the output:
Thread T1: lock1 acquired, waiting to acquire lock2. Thread T2: lock2 acquired, waiting to acquire lock1.
Once we run the program, we can see that the program results in a deadlock and never exits. The log shows that thread T1 is waiting for lock2, which is held by thread T2. Similarly, thread T2 is waiting for lock1, which is held by thread T1.
2.3. Avoiding Deadlock
Deadlock is a common concurrency problem in Java. Therefore, we should design a Java application to avoid any potential deadlock conditions.
To start with, we should avoid the need for acquiring multiple locks for a thread. However, if a thread does need multiple locks, we should make sure that each thread acquires the locks in the same order, to avoid any cyclic dependency in lock acquisition.
We can also use timed lock attempts, like the tryLock method in the Lock interface, to make sure that a thread does not block infinitely if it is unable to acquire a lock.
3. Livelock
3.1. What Is Livelock
Livelock is another concurrency problem and is similar to deadlock. In livelock, two or more threads keep on transferring states between one another instead of waiting infinitely as we saw in the deadlock example. Consequently, the threads are not able to perform their respective tasks.
A great example of livelock is a messaging system where, when an exception occurs, the message consumer rolls back the transaction and puts the message back to the head of the queue. Then the same message is repeatedly read from the queue, only to cause another exception and be put back on the queue. The consumer will never pick up any other message from the queue.
3.2. Livelock Example
Now, to demonstrate the livelock condition, we'll take the same deadlock example we've discussed earlier. In this example also, thread T1 calls operation1 and thread T2 calls operation2. However, we'll change the logic of these operations slightly.
Both threads need two locks to complete their work. Each thread acquires its first lock but finds that the second lock is not available. So, in order to let the other thread complete first, each thread releases its first lock and tries to acquire both the locks again.
Let's demonstrate livelock with a LivelockExample class:
public class LivelockExample { private Lock lock1 = new ReentrantLock(true); private Lock lock2 = new ReentrantLock(true); public static void main(String[] args) { LivelockExample livelock = new LivelockExample(); new Thread(livelock::operation1, "T1").start(); new Thread(livelock::operation2, "T2").start(); } public void operation1() { while (true) { tryLock(lock1, 50); print("lock1 acquired, trying to acquire lock2."); sleep(50); if (tryLock(lock2)) { print("lock2 acquired."); } else { print("cannot acquire lock2, releasing lock1."); lock1.unlock(); continue; } print("executing first operation."); break; } lock2.unlock(); lock1.unlock(); } public void operation2() { while (true) { tryLock(lock2, 50); print("lock2 acquired, trying to acquire lock1."); sleep(50); if (tryLock(lock1)) { print("lock1 acquired."); } else { print("cannot acquire lock1, releasing lock2."); lock2.unlock(); continue; } print("executing second operation."); break; } lock1.unlock(); lock2.unlock(); } // helper methods }
Now, let's run this example:
Thread T1: lock1 acquired, trying to acquire lock2. Thread T2: lock2 acquired, trying to acquire lock1. Thread T1: cannot acquire lock2, releasing lock1. Thread T2: cannot acquire lock1, releasing lock2. Thread T2: lock2 acquired, trying to acquire lock1. Thread T1: lock1 acquired, trying to acquire lock2. Thread T1: cannot acquire lock2, releasing lock1. Thread T1: lock1 acquired, trying to acquire lock2. Thread T2: cannot acquire lock1, releasing lock2. ..
As we can see in the logs, both the threads are repeatedly acquiring and releasing locks. Because of this, none of the threads are able to complete the operation.
3.3. Avoiding Livelock
To avoid a livelock, we need to look into the condition that is causing the livelock and then come up with a solution accordingly.
For example, if we have two threads that are repeatedly acquiring and releasing locks, resulting in livelock, we can design the code so that the threads retry acquiring the locks at random intervals. This will give the threads a fair chance to acquire the locks they need.
Another way to take care of the liveness problem in the messaging system example we've discussed earlier is to put failed messages in a separate queue for further processing instead of putting them back in the same queue again.
4. Conclusion
In this tutorial, we've discussed deadlock and livelock. Also, we've looked into Java examples to demonstrate each of these problems and briefly touched upon how we can avoid them.
As always, the complete code used in this example can be found over on GitHub.