国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

java - 多線程死鎖測試

瀏覽:84日期:2023-12-31 13:09:15

問題描述

package test;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.TimeUnit;/** * Created by rhwayfun on 16-4-3. */public class ThreadTest { private static DateFormat format = new SimpleDateFormat('HH:mm:ss'); public synchronized void tryOther(ThreadTest other) throws InterruptedException {System.out.println(Thread.currentThread().getName() + ' enter tryOther method at ' + format.format(new Date())); System.out.println(Thread.currentThread().getName() + ' tryOther method is about to invoke other method at ' + format.format(new Date()));other.other(); } public synchronized void other() throws InterruptedException {System.out.println(Thread.currentThread().getName() + ' enter other method atatatatat ' + format.format(new Date())); } public static void main(String[] args) throws InterruptedException {final ThreadTest d1 = new ThreadTest();final ThreadTest d2 = new ThreadTest();Thread t1 = new Thread(new Runnable() { public void run() {try { d1.tryOther(d2);} catch (InterruptedException e) { e.printStackTrace();} }}, 'threadA');Thread t2 = new Thread(new Runnable() { public void run() {try { d2.tryOther(d1);} catch (InterruptedException e) { e.printStackTrace();} }}, 'threadB');t1.start();//讓threadA先運行一秒TimeUnit.SECONDS.sleep(1);t2.start(); }}

如上,隨便找的產生死鎖的代碼,問題:TimeUnit.SECONDS.sleep(1);加上這行后,不存在死鎖問題。sleep并不釋放鎖,為何這邊死鎖情況會消失。輸出結果為:threadA enter tryOther method at 15:37:39threadA tryOther method is about to invoke other method at 15:37:39threadA enter other method atatatatat 15:37:39threadB enter tryOther method at 15:37:40threadB tryOther method is about to invoke other method at 15:37:40threadB enter other method atatatatat 15:37:40

注掉這行,正常死鎖。輸出結果為:threadB enter tryOther method at 15:37:10threadA enter tryOther method at 15:37:10threadB tryOther method is about to invoke other method at 15:37:10threadA tryOther method is about to invoke other method at 15:37:10

問題解答

回答1:

線程A拿到tryOther鎖但是他還要得到other的鎖線程B拿到tryOther的鎖但是他還要拿到other的鎖 有可能A剛剛釋放鎖B也剛剛釋放tryOther的鎖.此時但是他們同時都想要獲取other的鎖 此時誰也不讓誰 發生死鎖解決方法讓倆個線程不要同時去搶第二把鎖.讓A停一會但是如果你把時間調成納秒級別 多次嘗試也會發生死鎖不建議這樣預防死鎖.如果并發量高的情況下.

回答2:

雙方在爭同一把鎖,不會死鎖啊

回答3:

自己突然知道為什么了,懶得刪帖子了。寫下我的看法,如果有錯,歡迎指正,輕噴在沒有sleep時,a線程啟動,完成tryOther方法,釋放鎖并去執行other方法,此時b獲得鎖執行tryOther方法,此時a在other方法中所需資源被b線程鎖住,b在執行完tryOther后需要獲得a資源,由此產生死鎖。

加上sleep后。在a執行tryOther方法釋放鎖,此時b線程并沒有執行,此時順利獲得other鎖。2s后b線程執行,無死鎖環境。

標簽: java
相關文章:
主站蜘蛛池模板: 毛片免费全部免费播放 | 另类视频综合 | 亚洲精品国产一区二区三区在 | 免费在线观看一区 | 特级深夜a级毛片免费观看 特级生活片 | 成人在线中文字幕 | 色香欲综合成人免费视频 | 一级做a爱视频 | 日韩免费高清一级毛片在线 | 99精品国产成人一区二区在线 | 成人国产在线24小时播放视频 | 国产a级三级三级三级中国 国产a级特黄的片子视频 | 国产精品理论片在线观看 | 在线成人精品国产区免费 | 亚洲精品中文字幕一区在线 | 成人免费看毛片 | 久久久精品免费热线观看 | 亚洲综合天堂 | 久久免费特黄毛片 | 在线a亚洲视频播放在线观看 | 亚洲国产成人久久一区二区三区 | 久久精品网站免费观看 | 欧美日韩顶级毛片www免费看 | 亚洲欧美日韩中文字幕在线一 | 日产国产精品久久久久久 | 国产免费一级高清淫曰本片 | 欧美一级在线观看 | 一级看片免费视频囗交 | 免费区欧美一级毛片精品 | 欧美一级高清免费播放 | 国内精品久久久久久久影视麻豆 | 日韩一级| 久久久亚洲欧美综合 | 三级理论手机在线观看视频 | 亚洲精品国产精品国自产 | 黄色片亚洲| av18在线播放 | 一区二区三区视频免费观看 | 日本久久草 | 日韩色吧| 久草视频免费在线观看 |