淺談java安全編碼指南之死鎖dead lock
不同的加鎖順序
我們來(lái)看一個(gè)不同加鎖順序的例子:
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;
}
}
}
}
}
上面的例子中,我們模擬一個(gè)轉(zhuǎn)賬的過程,amount用來(lái)表示用戶余額。transfer用來(lái)將當(dāng)前賬號(hào)的一部分金額轉(zhuǎn)移到目標(biāo)對(duì)象中。
為了保證在transfer的過程中,兩個(gè)賬戶不被別人修改,我們使用了兩個(gè)synchronized關(guān)鍵字,分別把transfer對(duì)象和目標(biāo)對(duì)象進(jìn)行鎖定。
看起來(lái)好像沒問題,但是我們沒有考慮在調(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();
上面的例子中,我們定義了兩個(gè)account,然后兩個(gè)賬戶互相轉(zhuǎn)賬,最后很有可能導(dǎo)致互相鎖定,最后產(chǎn)生死鎖。
使用private類變量
使用兩個(gè)sync會(huì)有順序的問題,那么有沒有辦法只是用一個(gè)sync就可以在所有的實(shí)例中同步呢?
有的,我們可以使用private的類變量,因?yàn)轭愖兞渴窃谒袑?shí)例中共享的,這樣一次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;
}
}
}
}
使用相同的Order
我們產(chǎn)生死鎖的原因是無(wú)法控制上鎖的順序,如果我們能夠控制上鎖的順序,是不是就不會(huì)產(chǎn)生死鎖了呢?
帶著這個(gè)思路,我們給對(duì)象再加上一個(gè)id字段:
private final long id; // 唯一ID,用來(lái)排序
private static final AtomicLong nextID = new AtomicLong(0); // 用來(lái)生成ID
public DiffLockWithOrder(int amount){
this.amount=amount;
this.id = nextID.getAndIncrement();
}
在初始化對(duì)象的時(shí)候,我們使用static的AtomicLong類來(lái)為每個(gè)對(duì)象生成唯一的ID。
在做transfer的時(shí)候,我們先比較兩個(gè)對(duì)象的ID大小,然后根據(jù)ID進(jìn)行排序,最后安裝順序進(jìn)行加鎖。這樣就能夠保證順序,從而避免死鎖。
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;
}
}
}
}
釋放掉已占有的鎖
死鎖是互相請(qǐng)求對(duì)方占用的鎖,但是對(duì)方的鎖一直沒有釋放,我們考慮一下,如果獲取不到鎖的時(shí)候,自動(dòng)釋放已占用的鎖是不是也可以解決死鎖的問題呢?
因?yàn)镽eentrantLock有一個(gè)tryLock()方法,我們可以使用這個(gè)方法來(lái)判斷是否能夠獲取到鎖,獲取不到就釋放已占有的鎖。
我們使用ReentrantLock來(lái)完成這個(gè)例子:
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();
}
}
//隨機(jī)sleep一定的時(shí)間,保證可以釋放掉鎖
Thread.sleep(1000+new Random(1000L).nextInt(1000));
}
}
}
我們把兩個(gè)tryLock方法在while循環(huán)中,如果不能獲取到鎖就循環(huán)遍歷。
以上就是淺談java安全編碼指南之死鎖dead lock的詳細(xì)內(nèi)容,更多關(guān)于java安全編碼指南之死鎖dead lock的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Servlet實(shí)現(xiàn)簡(jiǎn)單文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Servlet實(shí)現(xiàn)簡(jiǎn)單文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
springmvc @ResponseStatus和ResponseEntity的使用
這篇文章主要介紹了springmvc @ResponseStatus和ResponseEntity的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能
對(duì)于大文件的處理,無(wú)論是用戶端還是服務(wù)端,如果一次性進(jìn)行讀取發(fā)送、接收都是不可取,很容易導(dǎo)致內(nèi)存問題,下面這篇文章主要給大家介紹了關(guān)于springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能的相關(guān)資料,需要的朋友可以參考下2023-06-06
深入解析Java類加載的案例與實(shí)戰(zhàn)教程
本篇文章主要介紹Tomcat類加載器架構(gòu),以及基于類加載和字節(jié)碼相關(guān)知識(shí),去分析動(dòng)態(tài)代理的原理,對(duì)Java類加載相關(guān)知識(shí)感興趣的朋友一起看看吧2022-05-05
微信公眾平臺(tái)開發(fā)實(shí)戰(zhàn)Java版之微信獲取用戶基本信息
這篇文章主要介紹了微信公眾平臺(tái)開發(fā)實(shí)戰(zhàn)Java版之微信獲取用戶基本信息 的相關(guān)資料,需要的朋友可以參考下2015-12-12
SpringBoot實(shí)現(xiàn)EMQ設(shè)備的上下線告警
EMQX?的上下線系統(tǒng)消息通知功能在客戶端連接成功或者客戶端斷開連接,需要實(shí)現(xiàn)設(shè)備的上下線狀態(tài)監(jiān)控,所以本文給大家介紹了如何通過SpringBoot實(shí)現(xiàn)EMQ設(shè)備的上下線告警,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10

