1. 概述
多线程在提高性能的同时,也带来了一些并发问题。本文我们将通过Java示例程序,理解多线程中的死锁(deadlock)和活锁(livelock)问题。
2. Deadlock 死锁
2.1. 什么是死锁?
死锁 发生在 两个及以上的线程,双方都在等待对方释放锁或资源,但是没有一方提前退出时,就称为死锁。
例如,一个进程p1占用了显示器,同时又必须使用打印机,而打印机被进程p2占用,p2又必须使用显示器,这样就形成了死锁。
经典的哲学家就餐问题很好地说明了多线程环境中的同步问题,并且经常被用作死锁的一个例子。
2.2. 死锁示例
First, let’s take a look into a simple Java example to understand deadlock.
本例中,我们创建2个线程 T1 和 *T2。线程T1 调用 operation1 方法, 线程T2 调用 operation2 方法。
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. 如何避免死锁
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. 什么是 Livelock
活锁与死锁相似,死锁是行程都在等待对方先释放资源;活锁则是进程彼此释放资源又同时占用对方释放的资源。白话文:
- 死锁:两人互不相让,都在等对方先让开。
- 活锁:两人互相礼让,却恰巧站到同一侧,再次让开,又站到同一侧,同样的情况不断重复下去导致双方都无法通过。
一个很好的活锁例子就是消息队列,当发生异常时,消费者会回滚事务并将消息放回队列的头部。然后,同一条消息被重复从队列中读取,结果却导致另一个异常并被放回队列。消费者永远不会从队列中获取任何其他消息。
3.2. Livelock 示例
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. 如何避免 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. 总结
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.