這篇文章主要介紹“java死鎖舉例分析”,在日常操作中,相信很多人在java死鎖舉例分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java死鎖舉例分析”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都網(wǎng)站設計、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元西盟做網(wǎng)站,已為上家服務,為西盟各地企業(yè)和個人服務,聯(lián)系電話:18980820575
java中為了保證共享數(shù)據(jù)的安全性,我們引入了鎖的機制。有了鎖就有可能產(chǎn)生死鎖。
死鎖的原因就是多個線程鎖住了對方所需要的資源,然后現(xiàn)有的資源又沒有釋放,從而導致循環(huán)等待的情況。
通常來說如果不同的線程對加鎖和釋放鎖的順序不一致的話,就很有可能產(chǎn)生死鎖。
我們來看一個不同加鎖順序的例子:
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用來將當前賬號的一部分金額轉移到目標對象中。
為了保證在transfer的過程中,兩個賬戶不被別人修改,我們使用了兩個synchronized關鍵字,分別把transfer對象和目標對象進行鎖定。
看起來好像沒問題,但是我們沒有考慮在調(diào)用的過程中,transfer的順序是可以發(fā)生變化的:
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,然后兩個賬戶互相轉賬,最后很有可能導致互相鎖定,最后產(chǎn)生死鎖。
使用兩個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; } } } }
我們產(chǎn)生死鎖的原因是無法控制上鎖的順序,如果我們能夠控制上鎖的順序,是不是就不會產(chǎn)生死鎖了呢?
帶著這個思路,我們給對象再加上一個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大小,然后根據(jù)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循環(huán)中,如果不能獲取到鎖就循環(huán)遍歷。
到此,關于“java死鎖舉例分析”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
本文題目:java死鎖舉例分析
分享網(wǎng)址:http://muchs.cn/article0/jcjcoo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、、建站公司、網(wǎng)站維護、品牌網(wǎng)站設計、網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)