認識線程死鎖
多個線程同時被阻塞,它們中的一個或者全部都在等待某個資源被釋放。由於線程被無限期地阻塞,因此程序不可能正常終止。
如下圖所示,線程 A 持有資源 2,線程 B 持有資源 1,他們同時都想申請對方的資源,所以這兩個線程就會互相等待而進入死鎖狀態。
死鎖示意圖
下面通過一個例子來說明線程死鎖,代碼模擬了上圖的死鎖的情況 (代碼來源於《並發編程之美》):
1 public class DeadLockDemo { 2 private static Object resource1 = new Object();//資源 1 3 private static Object resource2 = new Object();//資源 2 4 5 public static void main(String[] args) { 6 new Thread(() -> { 7 synchronized (resource1) { 8 System.out.println(Thread.currentThread() + "get resource1"); 9 try { 10 Thread.sleep(1000); 11 } catch (InterruptedException e) { 12 e.printStackTrace(); 13 } 14 System.out.println(Thread.currentThread() + "waiting get resource2"); 15 synchronized (resource2) { 16 System.out.println(Thread.currentThread() + "get resource2"); 17 } 18 } 19 }, "線程 1").start(); 20 21 new Thread(() -> { 22 synchronized (resource2) { 23 System.out.println(Thread.currentThread() + "get resource2"); 24 try { 25 Thread.sleep(1000); 26 } catch (InterruptedException e) { 27 e.printStackTrace(); 28 } 29 System.out.println(Thread.currentThread() + "waiting get resource1"); 30 synchronized (resource1) { 31 System.out.println(Thread.currentThread() + "get resource1"); 32 } 33 } 34 }, "線程 2").start(); 35 } 36 }
Output
1 Thread[線程 1,5,main]get resource1 2 Thread[線程 2,5,main]get resource2 3 Thread[線程 1,5,main]waiting get resource2 4 Thread[線程 2,5,main]waiting get resource1
線程 A 通過 synchronized (resource1)
獲得 resource1 的監視器鎖,然后通過Thread.sleep(1000);
讓線程 A 休眠 1s 為的是讓線程 B 得到執行然后獲取到 resource2 的監視器鎖。線程 A 和線程 B 休眠結束了都開始企圖請求獲取對方的資源,然后這兩個線程就會陷入互相等待的狀態,這也就產生了死鎖。上面的例子符合產生死鎖的四個必要條件。
學過操作系統的朋友都知道產生死鎖必須具備以下四個條件:
-
互斥條件:該資源任意一個時刻只由一個線程占用。
-
請求與保持條件:一個進程因請求資源而阻塞時,對已獲得的資源保持不放。
-
不剝奪條件:線程已獲得的資源在末使用完之前不能被其他線程強行剝奪,只有自己使用完畢后才釋放資源。
-
循環等待條件:若干進程之間形成一種頭尾相接的循環等待資源關系。
如何避免線程死鎖?
破壞互斥條件
這個條件我們沒有辦法破壞,因為我們用鎖本來就是想讓他們互斥的(臨界資源需要互斥訪問)。
破壞請求與保持條件
一次性申請所有的資源。
破壞不剝奪條件
占用部分資源的線程進一步申請其他資源時,如果申請不到,可以主動釋放它占有的資源。
破壞循環等待條件
靠按序申請資源來預防。按某一順序申請資源,釋放資源則反序釋放。破壞循環等待條件。
我們對線程 2 的代碼修改成下面這樣就不會產生死鎖了。
1 new Thread(() -> { 2 synchronized (resource1) { 3 System.out.println(Thread.currentThread() + "get resource1"); 4 try { 5 Thread.sleep(1000); 6 } catch (InterruptedException e) { 7 e.printStackTrace(); 8 } 9 System.out.println(Thread.currentThread() + "waiting get resource2"); 10 synchronized (resource2) { 11 System.out.println(Thread.currentThread() + "get resource2"); 12 } 13 } 14 }, "線程 2").start();
Output
1 Thread[線程 1,5,main]get resource1 2 Thread[線程 1,5,main]waiting get resource2 3 Thread[線程 1,5,main]get resource2 4 Thread[線程 2,5,main]get resource1 5 Thread[線程 2,5,main]waiting get resource2 6 Thread[線程 2,5,main]get resource2 7 8 Process finished with exit code 0
我們分析一下上面的代碼為什么避免了死鎖的發生?
線程 1 首先獲得到 resource1 的監視器鎖,這時候線程 2 就獲取不到了。然后線程 1 再去獲取 resource2 的監視器鎖,可以獲取到。然后線程 1 釋放了對 resource1、resource2 的監視器鎖的占用,線程 2 獲取到就可以執行了。這樣就破壞了破壞循環等待條件,因此避免了死鎖。
本文轉載自公眾號:Hollis 作者:澤林