在這篇文章中我將分享幾種常用的提高java并發編程中鎖的性能的技術,包括分離鎖、并行數據結構、保護數據而非代碼、縮小鎖的作用范圍,這幾種技術可以使我們不使用任何工具來檢測死鎖。
因此,明白競爭鎖和非競爭鎖的不同是非常重要的。當一個線程試圖進入 另一個線程正在執行的同步塊或方法時會觸發鎖競爭。該線程會被強制進入等待狀態,直到第一個線程執行完同步塊并且已經釋放了監視器。當同一時間只有一個線 程嘗試執行同步的代碼區域時,鎖會保持非競爭的狀態。事實上,在非競爭的情況下和大多數的應用中,JVM已經對同步進行了優化。非競爭鎖在執行過程中不會帶來任何額外的開銷。因此,你不應該因為性能問題抱怨鎖,應該抱怨的是鎖的競爭。當有了這個認識之后,讓我們來看下能做些什么,以降低競爭的可能性或減少競爭的持續時間。
解決線程安全問題的一個快速的方法就是對整個方法的可訪問性加鎖。例如下面這個例子,試圖通過這種方法來建立一個在線撲克游戲服務器:
class GameServer { public Map<<String, List<Player>> tables = new HashMap<String, List<Player>>(); public synchronized void join(Player player, Table table) { if (player.getAccountBalance() > table.getLimit()) { List<Player> tablePlayers = tables.get(table.getId()); if (tablePlayers.size() < 9) { tablePlayers.add(player); } } } public synchronized void leave(Player player, Table table) {/*body skipped for brevity*/} public synchronized void createTable() {/*body skipped for brevity*/} public synchronized void destroyTable(Table table) {/*body skipped for brevity*/}}作者的意圖是好的——當一個新的玩家加入牌桌 時,必須確保牌桌上的玩家個數不會超過牌桌可以容納的玩家總個數9。但是這種解決辦法事實上無論何時都要對玩家進入牌桌進行控制——即使是在服務器的訪問量較小的時候也是這樣,那些等 待鎖釋放的線程注定會頻繁的觸發系統的競爭事件。包含對賬戶余額和牌桌限制檢查的鎖定塊很可能大幅提高調用操作的開銷,而這無疑會增加競爭的可能性和持續時間。解決的第一步就是確保我們保護的是數據,而不是從方法聲明移到方法體中的那段同步聲明。對于上面那個簡單的例子來說,可能改變不大。但是我們要站在整個游戲服務的接口之上來考慮,而不是單單的一個join()方法。
class GameServer { public Map<String, List<Player>> tables = new HashMap<String, List<Player>>(); public void join(Player player, Table table) { synchronized (tables) { if (player.getAccountBalance() > table.getLimit()) { List<Player> tablePlayers = tables.get(table.getId()); if (tablePlayers.size() < 9) { tablePlayers.add(player); } } } } public void leave(Player player, Table table) {/* body skipped for brevity */} public void createTable() {/* body skipped for brevity */} public void destroyTable(Table table) {/* body skipped for brevity */}}原本可能只是一個小小的改變,影響的可是整個類的行為方式。玩家無論何時加入牌桌,先前的同步方法都會對整個GameServer實例加鎖,進而會與那些同時試圖離開牌桌的玩家產生競爭。將鎖從方法聲明移到方法體中會延遲鎖的加載,進而降低了鎖競爭的可能性。縮小鎖的作用范圍
現在,當確信了需要保護的是數據而非程序后,我們應該確保我們只在必要的地方加鎖——例如當上面的代碼被重構之后:public class GameServer { public Map<String, List<Player>> tables = new HashMap<String, List<Player>>(); public void join(Player player, Table table) { if (player.getAccountBalance() > table.getLimit()) { synchronized (tables) { List<Player> tablePlayers = tables.get(table.getId()); if (tablePlayers.size() < 9) { tablePlayers.add(player); } } } } //other methods skipped for brevity}這樣那段包含對玩家賬號余額檢測(可能引發IO操作)的可能引起費時操作的代碼,被移到了鎖控制的范圍之外。注意,現在鎖僅僅被用來防止玩家人數超過桌子可容納的人數,對賬戶余額的檢查不再是該保護措施的一部分了。分離鎖
你可以從上面例子最后一行代碼清楚的看到:整個數據結構是由相同的鎖保護著。考慮到在這一種數據結構中可能會有數以千計的牌桌,而我們必須保護任何一張牌桌的人數不超過容量,在這樣的情況下仍然會有很高的風險出現競爭事件。關于這個有一個簡單的辦法,就是對每一張牌桌引入分離鎖,如下面這個例子所示:
public class GameServer { public Map<String, List<Player>> tables = new HashMap<String, List<Player>>(); public void join(Player player, Table table) { if (player.getAccountBalance() > table.getLimit()) { List<Player> tablePlayers = tables.get(table.getId()); synchronized (tablePlayers) { if (tablePlayers.size() < 9) { tablePlayers.add(player); } } } } //other methods skipped for brevity}現在,我們只對單一牌桌的可訪問性進行同步而不是所有的牌桌,這樣就顯著降低了出現鎖競爭的可能性。舉一個具體的例子,現在在我們的數據結構中有100個牌桌的實例,那么現在發生競爭的可能性就會比之前小100倍。使用線程安全的數據結構
另一個可以改善的地方就是拋棄傳統的單線程數據結構,改用被明確設計為線程安全的數據結構。例如,當采用ConcurrentHashMap來儲存你的牌桌實例時,代碼可能像下面這樣:public class GameServer { public Map<String, List<Player>> tables = new ConcurrentHashMap<String, List<Player>>(); public void join(Player player, Table table) {/*Method body skipped for brevity*/} public void leave(Player player, Table table) {/*Method body skipped for brevity*/} public synchronized void createTable() { Table table = new Table(); tables.put(table.getId(), table); } public synchronized void destroyTable(Table table) { tables.remove(table.getId()); }}在join()和leave()方法內部的同步塊仍然和先前的例子一樣,因為我們要保證單個牌桌數據的完整性。ConcurrentHashMap 在這點上并沒有任何幫助。但我們仍然會在createTable()和destoryTable()方法中使用ConcurrentHashMap創建和銷毀新的牌桌,所有這些操作對于ConcurrentHashMap來說是完全同步的,其允許我們以并行的方式添加或減少牌桌的數量。其他一些建議和技巧
降低鎖的可見度。在上面的例子中,鎖被聲明為public(對外可見),這可能會使得一些別有用心的人通過在你精心設計的監視器上加鎖來破壞你的工作。通過查看java.util.concurrent.locks 的API來看一下 有沒有其它已經實現的鎖策略,使用其改進上面的解決方案。使用原子操作。在上面正在使用的簡單遞增計數器實際上并不要求加鎖。上面的例子中更適合使用 AtomicInteger代替Integer作為計數器。
新聞熱點
疑難解答