標籤:string 並且 body java 定時 AC amount stack hid
當一個線程永遠地持有一個鎖,並且其他線程都嘗試獲得這個鎖時,那麼他永遠被阻塞,當線程A持有鎖L並想獲得鎖M的同時,線程B持有鎖M並同時嘗試獲得鎖L時,那麼兩個線程將永遠的等待下去,這中情況就是簡單的死結的形式,其中多個線程由於存在環路的鎖依賴關係而永遠的等待下去,那麼就存在一個死結。
1、鎖順序死結
下面是順序鎖的一個列子,代碼如下:
1 package deadLock; 2 3 public class LeftRightDeadLock { 4 private final Object left = new Object(); 5 private final Object right = new Object(); 6 7 public void leftRight() throws Exception{ 8 synchronized (left) { 9 Thread.sleep(2000);10 synchronized (right) {11 System.out.println("left to right");12 }13 }14 }15 16 17 public void rightLeft() throws Exception{18 synchronized (right) {19 Thread.sleep(2000);20 synchronized (left) {21 System.out.println("right to left");22 }23 }24 }25 26 }View Code
1 package deadLock; 2 3 public class LeftRightThread extends Thread { 4 5 private LeftRightDeadLock d; 6 public LeftRightThread(LeftRightDeadLock d){ 7 this.d = d; 8 } 9 @Override10 public void run() {11 try{12 d.leftRight();13 }catch(Exception ex){14 ex.printStackTrace();15 }16 }17 18 }View Code
1 package deadLock; 2 3 public class RightLeftThread extends Thread { 4 5 private LeftRightDeadLock d; 6 public RightLeftThread(LeftRightDeadLock d){ 7 this.d = d; 8 } 9 @Override10 public void run() {11 try{12 d.rightLeft();13 }catch(Exception ex){14 ex.printStackTrace();15 }16 }17 18 }View Code
1 package deadLock; 2 3 public class Main { 4 public static void main(String[] args) { 5 LeftRightDeadLock d = new LeftRightDeadLock(); 6 LeftRightThread t1 = new LeftRightThread(d); 7 RightLeftThread t2 = new RightLeftThread(d); 8 t1.start(); 9 t2.start();10 }11 }View Code
線程t1持有left的鎖,並嘗試擷取得right的鎖,而線程t2持有right的鎖,並嘗試獲得left的鎖,故產生死結。產生死結的原因是:兩個線程試圖以不同的順序來獲得相同的鎖,如果按照相同的順序來請求鎖,那麼就不會出現迴圈的加鎖依賴性,因此也就不會產生死結。
如果所有線程以固定的順序來獲得鎖,那麼在程式中就不會出現鎖順序死結的問題
2、動態鎖順序死結
有時候,並不能清楚的知道是否在鎖順序上有足夠的控制權來避免死結的發生,看如下轉賬的代碼。
1 public class TransferAccounts { 2 public void transferMoney(Account fromAccount, Account toAccount, double amount) throws Exception{ 3 synchronized (fromAccount) { 4 synchronized (toAccount) { 5 if(fromAccount.getBalance() - amount < 0){ 6 throw new Exception(); 7 } 8 else{ 9 fromAccount.setBalance(amount);10 toAccount.add(amount);11 }12 }13 }14 }15 }View Code
1 public class Account { 2 3 //金額 4 private double balance; 5 6 public double getBalance() { 7 return balance; 8 } 9 10 public void setBalance(double balance) {11 this.balance = balance;12 }13 public void add(double amount){14 balance += amount;15 }16 public void subtra(double amount){17 balance -= amount;18 }19 20 }View Code
上面代碼是資金從一個賬戶轉到另一個賬戶的簡單實現,在開始轉賬之前要獲得這兩個Account對象的鎖,以確保通過原子方式來更新兩個賬戶的中餘額。所有的線程似乎都是按照順序鎖來獲得鎖,但是事實上鎖的順序取決於轉給函數transferMoney參數順序,而這些參數又取決於外部的輸入,如果兩個線程同時調用transferMoney,其中一個線程從X向Y轉賬,另一個線程從Y向X轉賬,有可能發生死結:
線程A:transferMoney(xAccount, yAccount);
線程B:transferMoney(yAccount, xAccount);
為了防止這種情況發生,必須按照順序來擷取鎖。如下代碼:
1 public class TransferAccounts { 2 private static final Object tieLock = new Object(); 3 4 public void transfer(Account fromAccount, Account toAccount, 5 double amount) throws Exception { 6 if (fromAccount.getBalance() - amount < 0) { 7 throw new Exception(); 8 } else { 9 fromAccount.setBalance(amount);10 toAccount.add(amount);11 }12 }13 public void transferMoney(Account fromAccount, Account toAccount,14 double amount) throws Exception{15 int fromHash = fromAccount.hashCode();16 int toHash = toAccount.hashCode();17 if(fromHash < toHash){18 synchronized (fromAccount) {19 synchronized (toAccount) {20 transfer(fromAccount, toAccount, amount);21 }22 }23 }else if(fromHash > toHash){24 synchronized (toAccount) {25 synchronized (fromAccount) {26 transfer(fromAccount, toAccount, amount);27 }28 }29 }else {30 synchronized (tieLock) {31 synchronized (fromAccount) {32 synchronized (toAccount) {33 transfer(fromAccount, toAccount, amount);34 }35 }36 }37 }38 }39 40 }View Code
在極少數的情況下,兩個對象可能擁有相對的hashCode值,這時必須通過添加額外的鎖來,在獲得兩個Account的鎖之前,必須先獲得這個額外的鎖。從而消除死結的發生。
3、死結的避免與診斷
(1) 如果一個程式每次至多隻能擷取一個鎖,那麼就不會產生鎖順序死結。
(2) 如果必須擷取多個鎖,那麼設計時必須考慮鎖的順序,如果按照固定的順序來擷取鎖,就不會發生鎖順序死結。
(3)支援定時鎖,例如顯示使用Lock類中的定時tryLock功能來代替內建鎖,顯示鎖可以指定一個逾時時限,在等待超過某個時間後tryLock會返回一個失敗的資訊。
Java多線程——死結