当线程需要同时持有多个锁时,有可能产生死锁。斟酌以下情形:
线程A当前持有互斥所锁lock1,线程B当前持有互斥锁lock2。接下来,当线程A依然持有lock1时,它试图获得lock2,由于线程B正持有lock2,因此线程A会阻塞等待线程B对lock2的释放。如果此时线程B在持有lock2的时候,也在试图获得lock1,由于线程A正持有lock1,因此线程B会阻塞等待A对lock1的释放。2者都在等待对方所持有锁的释放,而2者却又都没释放自己所持有的锁,这时候2者便会1直阻塞下去。这类情形称为死锁。
下面给出1个两个线程间产生死锁的示例,以下:
-
public class Deadlock extends Object {
-
private String objID;
-
-
public Deadlock(String id) {
-
objID = id;
-
}
-
-
public synchronized void checkOther(Deadlock other) {
-
print("entering checkOther()");
-
try { Thread.sleep(2000); }
-
catch ( InterruptedException x ) { }
-
print("in checkOther() - about to " + "invoke 'other.action()'");
-
-
-
other.action();
-
print("leaving checkOther()");
-
}
-
-
public synchronized void action() {
-
print("entering action()");
-
try { Thread.sleep(500); }
-
catch ( InterruptedException x ) { }
-
print("leaving action()");
-
}
-
-
public void print(String msg) {
-
threadPrint("objID=" + objID + " - " + msg);
-
}
-
-
public static void threadPrint(String msg) {
-
String threadName = Thread.currentThread().getName();
-
System.out.println(threadName + ": " + msg);
-
}
-
-
public static void main(String[] args) {
-
final Deadlock obj1 = new Deadlock("obj1");
-
final Deadlock obj2 = new Deadlock("obj2");
-
-
Runnable runA = new Runnable() {
-
public void run() {
-
obj1.checkOther(obj2);
-
}
-
};
-
-
Thread threadA = new Thread(runA, "threadA");
-
threadA.start();
-
-
try { Thread.sleep(200); }
-
catch ( InterruptedException x ) { }
-
-
Runnable runB = new Runnable() {
-
public void run() {
-
obj2.checkOther(obj1);
-
}
-
};
-
-
Thread threadB = new Thread(runB, "threadB");
-
threadB.start();
-
-
try { Thread.sleep(5000); }
-
catch ( InterruptedException x ) { }
-
-
threadPrint("finished sleeping");
-
-
threadPrint("about to interrupt() threadA");
-
threadA.interrupt();
-
-
try { Thread.sleep(1000); }
-
catch ( InterruptedException x ) { }
-
-
threadPrint("about to interrupt() threadB");
-
threadB.interrupt();
-
-
try { Thread.sleep(1000); }
-
catch ( InterruptedException x ) { }
-
-
threadPrint("did that break the deadlock?");
-
}
-
}
运行结果以下:
从结果中可以看出,在履行到other.action()时,由于两个线程都在试图获得对方的锁,但对方都没有释放自己的锁,因此便产生了死锁,在主线程中试图中断两个线程,但都无果。
大部份代码其实不容易产生死锁,死锁可能在代码中隐藏相当长的时间,等待不常见的条件地产生,但即便是很小的几率,1旦产生,即可能造成毁灭性的破坏。避免死锁是1件困难的事,遵守以下原则有助于规避死锁:
1、只在必要的最短时间内持有锁,斟酌使用同步语句块代替全部同步方法;
2、尽可能编写不在同1时刻需要持有多个锁的代码,如果不可避免,则确保线程持有第2个锁的时间尽可能短暂;
3、创建和使用1个大锁来代替若干小锁,并把这个锁用于互斥,而不是用作单个对象的对象级别锁;