併發情況下synchronized死鎖

2021-09-08 16:58:16 字數 1170 閱讀 7731

存在缺陷的**:

public class datapropertyidandnamerepositoryimpl

integer standardid = 0;

mappropertyidmap = propertyidlocalcache.get(dataid);

if (propertyidmap != null)

return standardid;

}public synchronized boolean loadtocache() catch (exception e)

return publishflag;}}

存在缺陷的流程:

a. 綠色表示第乙個執行緒,藍色表示第二個執行緒。

b. 黃色模組的**為synchronized標記的**,併發情況下只會有乙個執行緒執行此方法。

c. 綠色執行緒執行到紫色模組時,藍色執行緒等待進入黃色模組。

d. 藍色執行緒執行拋異常,導致publishflag被置為false。

e. 此時再次有執行緒進入,判斷publishflag仍為false,因此導致重複不斷載入loadtocache.

修復後的**:

public class datapropertyidandnamerepositoryimpl

integer standardid = 0;

mappropertyidmap = propertyidlocalcache.get(dataid);

if (propertyidmap != null)

return standardid;

}public synchronized boolean loadtocache()

datapropertyidandname datapropertyidandname = datapropertyidandnameservice.querydatapropertyidandname();

publishflag = true;

} catch (exception e)

return publishflag;}}

併發情況下synchronized死鎖

存在缺陷的 public class datapropertyidandnamerepositoryimpl integer standardid 0 mappropertyidmap propertyidlocalcache.get dataid if propertyidmap null ret...

高併發情況下扣除庫存鎖表情況

toc 1.鎖表情景 查詢條件沒有索引時 總結起來就是兩個嚴重問題 1.扣庫存時沒走索引 2.在事務中,調第三方介面 sql create table gap id int,age int,primary key id select from gap insert into test gap id ...

高併發情況下 如何支撐大量的請求

幾點需要注意 盡量使用快取,包括使用者快取,資訊快取等,多花點記憶體來做快取,可以大量減少與資料庫的互動,提高效能。用jprofiler等工具找出效能瓶頸,減少額外的開銷。優化資料庫查詢語句,減少直接使用hibernate等工具的直接生成語句 僅耗時較長的查詢做優化 優化資料庫結構,多做索引,提高查...