淺談Java由于不當(dāng)?shù)膱?zhí)行順序?qū)е碌乃梨i
我們來討論一個(gè)經(jīng)常存在的賬戶轉(zhuǎn)賬的問題。賬戶A要轉(zhuǎn)賬給賬戶B。為了保證在轉(zhuǎn)賬的過程中A和B不被其他的線程意外的操作,我們需要給A和B加鎖,然后再進(jìn)行轉(zhuǎn)賬操作, 我們看下轉(zhuǎn)賬的代碼:
public void transferMoneyDeadLock(Account from,Account to, int amount) throws InsufficientAmountException { synchronized (from){synchronized (to){ transfer(from,to,amount);} }}private void transfer(Account from,Account to, int amount) throws InsufficientAmountException { if(from.getBalance() < amount){throw new InsufficientAmountException(); }else{from.debit(amount);to.credit(amount); }}
看起來上面的程序好像沒有問題,因?yàn)槲覀兘ofrom和to都加了鎖,程序應(yīng)該可以很完美的按照我們的要求來執(zhí)行。
那如果我們考慮下面的一個(gè)場(chǎng)景:
A:transferMoneyDeadLock(accountA, accountB, 20)B:transferMoneyDeadLock(accountB, accountA, 10)
如果A和B同時(shí)執(zhí)行,則可能會(huì)產(chǎn)生A獲得了accountA的鎖,而B獲得了accountB的鎖。從而后面的代碼無法繼續(xù)執(zhí)行,從而導(dǎo)致了死鎖。
對(duì)于這樣的情況,我們有沒有什么好辦法來處理呢?
加入不管參數(shù)怎么傳遞,我們都先lock accountA再lock accountB是不是就不會(huì)出現(xiàn)死鎖的問題了呢?
我們看下代碼實(shí)現(xiàn):
private void transfer(Account from,Account to, int amount) throws InsufficientAmountException { if(from.getBalance() < amount){throw new InsufficientAmountException(); }else{from.debit(amount);to.credit(amount); }}public void transferMoney(Account from,Account to, int amount) throws InsufficientAmountException { int fromHash= System.identityHashCode(from); int toHash = System.identityHashCode(to); if(fromHash < toHash){synchronized (from){ synchronized (to){transfer(from,to, amount); }} }else if(fromHash < toHash){synchronized (to){ synchronized (from){transfer(from,to, amount); }} }else{synchronized (lock){synchronized (from) { synchronized (to) {transfer(from, to, amount); } }} }}
上面的例子中,我們使用了System.identityHashCode來獲得兩個(gè)賬號(hào)的hash值,通過比較hash值的大小來選定lock的順序。
如果兩個(gè)賬號(hào)的hash值恰好相等的情況下,我們引入了一個(gè)新的外部lock,從而保證同一時(shí)間只有一個(gè)線程能夠運(yùn)行內(nèi)部的方法,從而保證了任務(wù)的執(zhí)行而不產(chǎn)生死鎖。
以上就是淺談Java由于不當(dāng)?shù)膱?zhí)行順序?qū)е碌乃梨i的詳細(xì)內(nèi)容,更多關(guān)于Java由于不當(dāng)?shù)膱?zhí)行順序?qū)е碌乃梨i的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟2. IntelliJ IDEA導(dǎo)入jar包的方法3. SSM框架JSP使用Layui實(shí)現(xiàn)layer彈出層效果4. 刪除docker里建立容器的操作方法5. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法6. 基于android studio的layout的xml文件的創(chuàng)建方式7. Python產(chǎn)生batch數(shù)據(jù)的操作8. Java導(dǎo)出Execl疑難點(diǎn)處理的實(shí)現(xiàn)9. 淺談定義一個(gè)PHP函數(shù)10. IDEA創(chuàng)建SpringBoot的maven項(xiàng)目的方法步驟
