研究僵局–第4部分:修復代碼

在這個簡短的博客系列的最后BadTransferOperation中,我一直在討論分析死鎖,我將修復BadTransferOperation代碼。 如果您看過本系列的其他博客 ,那么您將知道,為了達到這一點,我創建了死鎖的演示代碼,展示了如何掌握線程轉儲,然后分析了線程轉儲,弄清楚發生僵局的位置和方式。 為了節省空間,下面的討論同時引用了本系列第1部分中的AccountDeadlockDemo類,其中包含完整的代碼清單。

教科書中有關死鎖的描述通常是這樣的:“線程A將獲得對象1的鎖定,并等待對象2的鎖定,而線程B將獲得對象2的鎖定,同時等待對象1的鎖定”。 我以前的博客中顯示的堆積,并在下面突出顯示,是一個真實的死鎖,其他線程,鎖和對象陷入了直接,簡單,理論上的死鎖情況。

Found one Java-level deadlock:
=============================
'Thread-21':waiting to lock monitor 7f97118bd560 (object 7f3366f58, a threads.deadlock.Account),which is held by 'Thread-20'
'Thread-20':waiting to lock monitor 7f97118bc108 (object 7f3366e98, a threads.deadlock.Account),which is held by 'Thread-4'
'Thread-4':waiting to lock monitor 7f9711834360 (object 7f3366e80, a threads.deadlock.Account),which is held by 'Thread-7'
'Thread-7':waiting to lock monitor 7f97118b9708 (object 7f3366eb0, a threads.deadlock.Account),which is held by 'Thread-11'
'Thread-11':waiting to lock monitor 7f97118bd560 (object 7f3366f58, a threads.deadlock.Account),which is held by 'Thread-20'


如果將上面的文本和圖像與以下代碼相關聯,則可以看到Thread-20已鎖定其fromAccount對象( fromAccount ),正在等待鎖定其toAccount對象(e98)

private void transfer(Account fromAccount, Account toAccount, int transferAmount) throws OverdrawnException {synchronized (fromAccount) {synchronized (toAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}}

不幸的是,由于時序問題, Thread-20無法獲得對對象e98的鎖定,因為它正在等待Thread-4釋放對該對象的鎖定。 Thread-4無法釋放鎖,因為它正在等待Thread-7Thread-7正在等待Thread-11Thread-11正在等待Thread-20釋放對對象f58的鎖。 這個現實世界的僵局只是教科書描述的一個更復雜的版本。

這段代碼的問題是,從下面的代碼片段中,您可以看到我正在從Accounts數組中隨機選擇兩個Account對象作為fromAccounttoAccount并將它們鎖定。 由于fromAccounttoAccount可以引用accounts數組中的任何對象,這意味著它們以隨機順序被鎖定。

Account toAccount = accounts.get(rnd.nextInt(NUM_ACCOUNTS));Account fromAccount = accounts.get(rnd.nextInt(NUM_ACCOUNTS));

因此, 解決方法是對Account對象的鎖定方式施加順序,并且只要順序一致,任何順序都可以執行。

private void transfer(Account fromAccount, Account toAccount, int transferAmount) throws OverdrawnException {if (fromAccount.getNumber() > toAccount.getNumber()) {synchronized (fromAccount) {synchronized (toAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}} else {synchronized (toAccount) {synchronized (fromAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}}}

上面的代碼顯示了此修復程序。 在此代碼中,我使用帳號來確保首先鎖定具有最高帳號的Account對象,以便永遠不會出現以上的死鎖情況。

以下代碼是此修復程序的完整列表:

public class AvoidsDeadlockDemo {private static final int NUM_ACCOUNTS = 10;private static final int NUM_THREADS = 20;private static final int NUM_ITERATIONS = 100000;private static final int MAX_COLUMNS = 60;static final Random rnd = new Random();List<Account> accounts = new ArrayList<Account>();public static void main(String args[]) {AvoidsDeadlockDemo demo = new AvoidsDeadlockDemo();demo.setUp();demo.run();}void setUp() {for (int i = 0; i < NUM_ACCOUNTS; i++) {Account account = new Account(i, rnd.nextInt(1000));accounts.add(account);}}void run() {for (int i = 0; i < NUM_THREADS; i++) {new BadTransferOperation(i).start();}}class BadTransferOperation extends Thread {int threadNum;BadTransferOperation(int threadNum) {this.threadNum = threadNum;}@Overridepublic void run() {for (int i = 0; i < NUM_ITERATIONS; i++) {Account toAccount = accounts.get(rnd.nextInt(NUM_ACCOUNTS));Account fromAccount = accounts.get(rnd.nextInt(NUM_ACCOUNTS));int amount = rnd.nextInt(1000);if (!toAccount.equals(fromAccount)) {try {transfer(fromAccount, toAccount, amount);System.out.print(".");} catch (OverdrawnException e) {System.out.print("-");}printNewLine(i);}}System.out.println("Thread Complete: " + threadNum);}private void printNewLine(int columnNumber) {if (columnNumber % MAX_COLUMNS == 0) {System.out.print("\n");}}/*** This is the crucial point here. The idea is that to avoid deadlock you need to ensure that threads can't try* to lock the same two accounts in the same order*/private void transfer(Account fromAccount, Account toAccount, int transferAmount) throws OverdrawnException {if (fromAccount.getNumber() > toAccount.getNumber()) {synchronized (fromAccount) {synchronized (toAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}} else {synchronized (toAccount) {synchronized (fromAccount) {fromAccount.withdraw(transferAmount);toAccount.deposit(transferAmount);}}}}}
}

在我的示例代碼,死鎖的發生是因為時機問題,嵌套的synchronized在我的關鍵字BadTransferOperation類。 在此代碼中, synchronized關鍵字位于相鄰的行上; 但是,最后一點是,值得注意的是, synchronized關鍵字在代碼中的什么位置都沒關系(它們不必相鄰)。 只要您使用同一線程鎖定兩個(或更多)不同的監視對象,就會發生排序和死鎖。

有關更多信息,請參閱本系列中的其他博客 。

該系列以及其他博客的所有源代碼都可以在Github上找到,網址為git://github.com/roghughe/captaindebug.git

參考: 調查死鎖-第4部分:來自Captain Debug博客博客的JCG合作伙伴 Roger Hughes 修復代碼 。

翻譯自: https://www.javacodegeeks.com/2012/11/investigating-deadlocks-part-4-fixing-the-code.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/370433.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/370433.shtml
英文地址,請注明出處:http://en.pswp.cn/news/370433.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

chrome插件2

轉自&#xff1a;http://www.codeceo.com/article/15-chrome-extension.html 1. Web Developer 支持Chrome的Web Developer擴展&#xff0c;允許你通過添加一個小工具欄來使用不同的工具。 官方網站&#xff1a;https://chrome.google.com/webstore/detail/web-developer/bfbam…

java月歷組件_vue之手把手教你寫日歷組件

---恢復內容開始---1.日歷組件1.分析功能&#xff1a;日歷基本功能&#xff0c;點擊事件改變日期&#xff0c;樣式的改變1.結構分析&#xff1a;html1.分為上下兩個部分2.上面分為左按鈕&#xff0c;中間內容展示&#xff0c;右按鈕下面分為周幾展示和日期展示3.基本結構頁面ht…

HTML5和css3

超鏈接 <a target"頁面打開位置" href"鏈接地址">內容</a>target:_blank 重新打開一個頁面target:_self 當前頁面打開 1.頁面地址&#xff1a; 基礎功能&#xff0c;用于進入該鏈接的頁面&#xff1b; 2.錨點&#xff1a; 需要給標簽名定義id…

python下載顯示文件丟失_Microsoft.PythonTools.resources.dll

我該如何安裝從金山毒霸下載的DLL文件&#xff1f;一&#xff1a;1、從金山毒霸下載壓縮文件。2、將DLL文件解壓到電腦上的某個地方。3、把該文件跟要求使用它的程序放在同一路徑上。注意32位程序需要使用32位的DLL文件&#xff0c;64位程序需要使用64位的DLL文件。否則會出現0…

maven project module 依賴項目創建 ---轉

一、創建Maven Project 1.右擊 --> New --> Other&#xff0c;--> Maven --> Maven Project --> Next 2.如下圖&#xff0c;選中Create a simple project --> Next 3.輸入Group Id, Artifact Id, Version, Packaging選擇pom&#xff0c;因為創建的Maven Pr…

java soot_正確執行3個地址代碼的SOOT API

我在運行SOOT API時遇到問題 . 我正在使用java -cp soot-2.5.0.jar soot.Main -f jimple test我遇到以下錯誤&#xff1a;Exception in thread "main" java.lang.RuntimeException: Could not load classfile: java.io.ObjectInputStream atat soot.coffi.Util.resol…

JSF AJAX請求的會話超時處理

JSF AJAX請求的會話超時處理 當我們使用AJAX行為開發JSF應用程序時&#xff0c;在處理Ajax請求超時場景時可能會遇到問題。 例如&#xff0c;如果您使用的是基于J2EE表單的身份驗證&#xff0c;則會話超時后應將正常請求重定向到登錄頁面。 但是&#xff0c;如果您的請求是AJAX…

linux常見命令搜集

查找根目錄下txt和pdf文件 find / \( -name "*.txt" -o -name "*.pdf" \) -print 正則查找根目錄下所有的txt和pdf文件 find / -regex ".*\(\.txt|\.pdf\)$"查找所有非txt文本 find . ! -name "*.txt" -print制定搜索深度 find ~ -max…

前端html,css基礎總結

0.1、css引入界面的方式: 內聯式:通過標簽的style屬性&#xff0c;在標簽上直接寫樣式。 <div style"width:100px; height:100px; background:red "></div> 嵌入式:通過style標簽&#xff0c;在網頁上創建嵌入的樣式表。 <style type"text/css&q…

知乎python練手的_Python—爬蟲之初級實戰項目:爬取知乎任一作者的文章練手

爬蟲之初級實戰項目&#xff1a;爬取知乎任一作者的文章練手在正式上代碼之前&#xff0c;先過一遍之前所學知識的框架內容&#xff0c;溫故而知新&#xff01;&#xff01;&#xff01;接下來我們直接上代碼&#xff0c;一定要手敲代碼、手敲代碼、手敲代碼&#xff01;&#…

java url幫助類_Spring居然還提供了這么好用的URL工具類

1. 前言開發中我們經常會操作 URL&#xff0c;比如提取端口、提取路徑以及最常用的提取參數等等。很多時候需要借助于一些第三方類庫或者自己編寫工具類來實現&#xff0c;今天胖哥給大家介紹一種方法&#xff0c;無需新的類庫引入&#xff0c;只要你使用了 Spring Web 模塊都可…

Java并發之CyclicBarria的使用(二)

Java并發之CyclicBarria的使用&#xff08;二&#xff09; 一.簡介 之前借助于其他大神寫過一篇關于CyclicBarria用法的博文&#xff0c;但是內心總是感覺絲絲的愧疚&#xff0c;因為筆者喜歡原創&#xff0c;而不喜歡去轉載一些其他的文章&#xff0c;為此筆者自己原創了一個C…

需加裝飾——裝飾模式

裝飾模式指的是在不必改變原類文件和使用繼承的情況下&#xff0c;動態地擴展一個對象的功能。它是通過創建一個包裝對象&#xff0c;也就是裝飾來包裹真實的對象。 類圖分析 我們先假設一個業務場景&#xff0c;有三種房子需要裝修&#xff0c;分別是公寓&#xff0c;木屋和別…

Java正則表達式教程及示例

當我開始使用Java時&#xff0c;正則表達式對我來說是一場噩夢。 本教程旨在幫助您掌握Java正則表達式&#xff0c;并讓我定期返回以刷新我的正則表達式學習。 什么是正則表達式&#xff1f; 正則表達式定義字符串的模式。 正則表達式可用于搜索&#xff0c;編輯或處理文本。…

Vue2.0 --- vue-cli腳手架中全局引入JQ

第一步&#xff1a;安裝jQuery npm/cmpn方式安裝(默認安裝1.7.X版本的JQ) npm/cnpm install jQuery 如果想安裝更高版本的JQ那么可以選擇在package.json文件下面這個位置添加代碼斷&#xff08;當前圖片安裝的是2.2.3版本&#xff0c;如果想安裝更高或者其他可以更改版本號&…

python筆記全_Python筆記

一、數據結構和序列1.1、元組&#xff1a;有一種固定長度&#xff0c;不可修改的python對象序列tup 1,2,3 tup : (1,2,3)tup tuple([4,0,2]) tup : (4,0,2)tup[0] 4元組添加元素&#xff1a;tup (["foo",[1,2],True])tup[1].append(3)tup : ("foo",[1,…

java 分布式編譯_linux分布式編譯distcc和ccache的部署

unset LANGUAGEexport LANG"en"cd /home/kingsoftmkdir distcccd distccrpm包用&#xff1a;rpm -ivh ...bz2包用&#xff1a;tar -xvf ...進入distcc解壓后的目錄./configure && make && make installmkdir /usr/lib/distccmkdir /usr/lib/distcc/b…

Unity——用UnityEditor拷貝FBX中的AnimationClip

最近有個新需求&#xff0c;要用代碼添加動畫的事件&#xff0c;但是Unity不能直接修改FBX中的AnimationClip 在Animation窗口中可以看到&#xff0c;AnimationClip是Read-Only狀態&#xff0c;用代碼修改這個AnimationClip也是不會生效的&#xff0c;包括用代碼添加事件 解決方…

sql 分頁存儲過程

ALTER procedure [dbo].[fenye]pagesize int, --每頁顯示數量pageCurrent int, --當前頁tablename varchar(20), --表名field varchar(20), --顯示的列名(eg: id,name)where varchar(20), --篩選條件 (eg: name not null)orderBy varchar(20), --排序的列名&#xff08;eg: id …

使用Hadoop計算共現矩陣

這篇文章繼續我們在MapReduce的數據密集型文本處理一書中實現MapReduce算法的系列。 這次&#xff0c;我們將從文本語料庫創建單詞共現矩陣。 本系列以前的文章是&#xff1a; 使用MapReduce進行數據密集型文本處理 使用MapReduce進行數據密集型文本處理-本地聚合第二部分 共…