大剑无锋之什么是死锁?【面试推荐】
什么是線程死鎖?如何避免死鎖?
1. 認識線程死鎖
多個線程同時被阻塞,它們中的一個或者全部都在等待某個資源被釋放。由于線程被無限期地阻塞,因此程序不可能正常終止。
如下圖所示,線程 A 持有資源 2,線程 B 持有資源 1,他們同時都想申請對方的資源,所以這兩個線程就會互相等待而進入死鎖狀態。
線程死鎖示意圖
下面通過一個例子來說明線程死鎖,代碼模擬了上圖的死鎖的情況 (代碼來源于《并發編程之美》):
public class DeadLockDemo {private static Object resource1 = new Object();//資源 1private static Object resource2 = new Object();//資源 2public static void main(String[] args) {new Thread(() -> {synchronized (resource1) {System.out.println(Thread.currentThread() + "get resource1");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread() + "waiting get resource2");synchronized (resource2) {System.out.println(Thread.currentThread() + "get resource2");}}}, "線程 1").start();new Thread(() -> {synchronized (resource2) {System.out.println(Thread.currentThread() + "get resource2");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread() + "waiting get resource1");synchronized (resource1) {System.out.println(Thread.currentThread() + "get resource1");}}}, "線程 2").start();} }Output
Thread[線程 1,5,main]get resource1 Thread[線程 2,5,main]get resource2 Thread[線程 1,5,main]waiting get resource2 Thread[線程 2,5,main]waiting get resource1線程 A 通過 synchronized (resource1) 獲得 resource1 的監視器鎖,然后通過Thread.sleep(1000);讓線程 A 休眠 1s 為的是讓線程 B 得到執行然后獲取到 resource2 的監視器鎖。線程 A 和線程 B 休眠結束了都開始企圖請求獲取對方的資源,然后這兩個線程就會陷入互相等待的狀態,這也就產生了死鎖。上面的例子符合產生死鎖的四個必要條件。
學過操作系統的朋友都知道產生死鎖必須具備以下四個條件:
互斥條件:該資源任意一個時刻只由一個線程占用。
請求與保持條件:一個進程因請求資源而阻塞時,對已獲得的資源保持不放。
不剝奪條件:線程已獲得的資源在末使用完之前不能被其他線程強行剝奪,只有自己使用完畢后才釋放資源。
循環等待條件:若干進程之間形成一種頭尾相接的循環等待資源關系。
2. 如何避免線程死鎖?
我們只要破壞產生死鎖的四個條件中的其中一個就可以了。
破壞互斥條件
這個條件我們沒有辦法破壞,因為我們用鎖本來就是想讓他們互斥的(臨界資源需要互斥訪問)。
破壞請求與保持條件
一次性申請所有的資源。
破壞不剝奪條件
占用部分資源的線程進一步申請其他資源時,如果申請不到,可以主動釋放它占有的資源。
破壞循環等待條件
靠按序申請資源來預防。按某一順序申請資源,釋放資源則反序釋放。破壞循環等待條件。
我們對線程 2 的代碼修改成下面這樣就不會產生死鎖了。
new Thread(() -> {synchronized (resource1) {System.out.println(Thread.currentThread() + "get resource1");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread() + "waiting get resource2");synchronized (resource2) {System.out.println(Thread.currentThread() + "get resource2");}}}, "線程 2").start();Output
Thread[線程 1,5,main]get resource1 Thread[線程 1,5,main]waiting get resource2 Thread[線程 1,5,main]get resource2 Thread[線程 2,5,main]get resource1 Thread[線程 2,5,main]waiting get resource2 Thread[線程 2,5,main]get resource2Process finished with exit code 0我們分析一下上面的代碼為什么避免了死鎖的發生?
線程 1 首先獲得到 resource1 的監視器鎖,這時候線程 2 就獲取不到了。然后線程 1 再去獲取 resource2 的監視器鎖,可以獲取到。然后線程 1 釋放了對 resource1、resource2 的監視器鎖的占用,線程 2 獲取到就可以執行了。這樣就破壞了破壞循環等待條件,因此避免了死鎖。
總結
以上是生活随笔為你收集整理的大剑无锋之什么是死锁?【面试推荐】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大剑无锋之简单说一下聚簇索引和非聚簇索引
- 下一篇: 每日两SQL(1),欢迎交流~