java中爲了保證共享數據的安全性,咱們引入了鎖的機制。有了鎖就有可能產生死鎖。java
死鎖的緣由就是多個線程鎖住了對方所須要的資源,而後現有的資源又沒有釋放,從而致使循環等待的狀況。git
一般來講若是不一樣的線程對加鎖和釋放鎖的順序不一致的話,就頗有可能產生死鎖。github
咱們來看一個不一樣加鎖順序的例子:安全
public class DiffLockOrder { private int amount; public DiffLockOrder(int amount){ this.amount=amount; } public void transfer(DiffLockOrder target,int transferAmount){ synchronized (this){ synchronized (target){ if(amount< transferAmount){ System.out.println("餘額不足!"); }else{ amount=amount-transferAmount; target.amount=target.amount+transferAmount; } } } } }
上面的例子中,咱們模擬一個轉帳的過程,amount用來表示用戶餘額。transfer用來將當前帳號的一部分金額轉移到目標對象中。dom
爲了保證在transfer的過程當中,兩個帳戶不被別人修改,咱們使用了兩個synchronized關鍵字,分別把transfer對象和目標對象進行鎖定。this
看起來好像沒問題,可是咱們沒有考慮在調用的過程當中,transfer的順序是能夠發送變化的:線程
DiffLockOrder account1 = new DiffLockOrder(1000); DiffLockOrder account2 = new DiffLockOrder(500); Runnable target1= ()->account1.transfer(account2,200); Runnable target2= ()->account2.transfer(account1,100); new Thread(target1).start(); new Thread(target2).start();
上面的例子中,咱們定義了兩個account,而後兩個帳戶互相轉帳,最後頗有可能致使互相鎖定,最後產生死鎖。code
使用兩個sync會有順序的問題,那麼有沒有辦法只是用一個sync就能夠在全部的實例中同步呢?對象
有的,咱們可使用private的類變量,由於類變量是在全部實例中共享的,這樣一次sync就夠了:排序
public class LockWithPrivateStatic { private int amount; private static final Object lock = new Object(); public LockWithPrivateStatic(int amount){ this.amount=amount; } public void transfer(LockWithPrivateStatic target, int transferAmount){ synchronized (lock) { if (amount < transferAmount) { System.out.println("餘額不足!"); } else { amount = amount - transferAmount; target.amount = target.amount + transferAmount; } } } }
咱們產生死鎖的緣由是沒法控制上鎖的順序,若是咱們可以控制上鎖的順序,是否是就不會產生死鎖了呢?
帶着這個思路,咱們給對象再加上一個id字段:
private final long id; // 惟一ID,用來排序 private static final AtomicLong nextID = new AtomicLong(0); // 用來生成ID public DiffLockWithOrder(int amount){ this.amount=amount; this.id = nextID.getAndIncrement(); }
在初始化對象的時候,咱們使用static的AtomicLong類來爲每一個對象生成惟一的ID。
在作transfer的時候,咱們先比較兩個對象的ID大小,而後根據ID進行排序,最後安裝順序進行加鎖。這樣就可以保證順序,從而避免死鎖。
public void transfer(DiffLockWithOrder target, int transferAmount){ DiffLockWithOrder fist, second; if (compareTo(target) < 0) { fist = this; second = target; } else { fist = target; second = this; } synchronized (fist){ synchronized (second){ if(amount< transferAmount){ System.out.println("餘額不足!"); }else{ amount=amount-transferAmount; target.amount=target.amount+transferAmount; } } } }
死鎖是互相請求對方佔用的鎖,可是對方的鎖一直沒有釋放,咱們考慮一下,若是獲取不到鎖的時候,自動釋放已佔用的鎖是否是也能夠解決死鎖的問題呢?
由於ReentrantLock有一個tryLock()方法,咱們可使用這個方法來判斷是否可以獲取到鎖,獲取不到就釋放已佔有的鎖。
咱們使用ReentrantLock來完成這個例子:
public class DiffLockWithReentrantLock { private int amount; private final Lock lock = new ReentrantLock(); public DiffLockWithReentrantLock(int amount){ this.amount=amount; } private void transfer(DiffLockWithReentrantLock target, int transferAmount) throws InterruptedException { while (true) { if (this.lock.tryLock()) { try { if (target.lock.tryLock()) { try { if(amount< transferAmount){ System.out.println("餘額不足!"); }else{ amount=amount-transferAmount; target.amount=target.amount+transferAmount; } break; } finally { target.lock.unlock(); } } } finally { this.lock.unlock(); } } //隨機sleep必定的時間,保證能夠釋放掉鎖 Thread.sleep(1000+new Random(1000L).nextInt(1000)); } } }
咱們把兩個tryLock方法在while循環中,若是不能獲取到鎖就循環遍歷。
本文的代碼:
learn-java-base-9-to-20/tree/master/security
本文已收錄於 http://www.flydean.com/java-security-code-line-dead-lock/最通俗的解讀,最深入的乾貨,最簡潔的教程,衆多你不知道的小技巧等你來發現!
歡迎關注個人公衆號:「程序那些事」,懂技術,更懂你!