当线程需要同时持有多个锁时,有可能产生死锁。考虑如下情形:
线程A当前持有互斥所锁lock1,线程B当前持有互斥锁lock2。接下来,当线程A仍然持有lock1时,它试图获取lock2,因为线程B正持有lock2,因此线程A会阻塞等待线程B对lock2的释放。如果此时线程B在持有lock2的时候,也在试图获取lock1,因为线程A正持有lock1,因此线程B会阻塞等待A对lock1的释放。二者都在等待对方所持有锁的释放,而二者却又都没释放自己所持有的锁,这时二者便会一直阻塞下去。这种情形称为死锁。
下面给出一个两个线程间产生死锁的示例,如下:
1 public class Deadlock extends Object { 2 private String objID; 3 4 public Deadlock(String id) { 5 objID = id; 6 } 7 8 public synchronized void checkOther(Deadlock other) { 9 print("entering checkOther()"); 10 try { Thread.sleep(2000); } 11 catch ( InterruptedException x ) { } 12 print("in checkOther() - about to " + "invoke 'other.action()'"); 13 14 //调用other对象的action方法,由于该方法是同步方法,因此会试图获取other对象的对象锁 15 other.action(); 16 print("leaving checkOther()"); 17 } 18 19 public synchronized void action() { 20 print("entering action()"); 21 try { Thread.sleep(500); } 22 catch ( InterruptedException x ) { } 23 print("leaving action()"); 24 } 25 26 public void print(String msg) { 27 threadPrint("objID=" + objID + " - " + msg); 28 } 29 30 public static void threadPrint(String msg) { 31 String threadName = Thread.currentThread().getName(); 32 System.out.println(threadName + ": " + msg); 33 } 34 35 public static void main(String[] args) { 36 final Deadlock obj1 = new Deadlock("obj1"); 37 final Deadlock obj2 = new Deadlock("obj2"); 38 39 Runnable runA = new Runnable() { 40 public void run() { 41 obj1.checkOther(obj2); 42 } 43 }; 44 45 Thread threadA = new Thread(runA, "threadA"); 46 threadA.start(); 47 48 try { Thread.sleep(200); } 49 catch ( InterruptedException x ) { } 50 51 Runnable runB = new Runnable() { 52 public void run() { 53 obj2.checkOther(obj1); 54 } 55 }; 56 57 Thread threadB = new Thread(runB, "threadB"); 58 threadB.start(); 59 60 try { Thread.sleep(5000); } 61 catch ( InterruptedException x ) { } 62 63 threadPrint("finished sleeping"); 64 65 threadPrint("about to interrupt() threadA"); 66 threadA.interrupt(); 67 68 try { Thread.sleep(1000); } 69 catch ( InterruptedException x ) { } 70 71 threadPrint("about to interrupt() threadB"); 72 threadB.interrupt(); 73 74 try { Thread.sleep(1000); } 75 catch ( InterruptedException x ) { } 76 77 threadPrint("did that break the deadlock?"); 78 } 79 }
运行结果如下:
从结果中可以看出,在执行到other.action()时,由于两个线程都在试图获取对方的锁,但对方都没有释放自己的锁,因而便产生了死锁,在主线程中试图中断两个线程,但都无果。
大部分代码并不容易产生死锁,死锁可能在代码中隐藏相当长的时间,等待不常见的条件地发生,但即使是很小的概率,一旦发生,便可能造成毁灭性的破坏。避免死锁是一件困难的事,遵循以下原则有助于规避死锁:
1、只在必要的最短时间内持有锁,考虑使用同步语句块代替整个同步方法;
2、尽量编写不在同一时刻需要持有多个锁的代码,如果不可避免,则确保线程持有第二个锁的时间尽量短暂;
3、创建和使用一个大锁来代替若干小锁,并把这个锁用于互斥,而不是用作单个对象的对象级别锁;