av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

java - 多線程死鎖測試

瀏覽:109日期: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的鎖 此時誰也不讓誰 發(fā)生死鎖解決方法讓倆個線程不要同時去搶第二把鎖.讓A停一會但是如果你把時間調成納秒級別 多次嘗試也會發(fā)生死鎖不建議這樣預防死鎖.如果并發(fā)量高的情況下.

回答2:

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

回答3:

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

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

標簽: java
相關文章:
主站蜘蛛池模板: 男女精品网站 | 成人av免费看 | 欧美日韩理论 | 久久久久免费观看 | 日韩欧美不卡 | 国产一区二区三区四区在线观看 | 99视频在线 | 九九热免费观看 | 国产精品观看 | 精品成人免费视频 | 亚洲黄色成人网 | 久久久久久久久国产精品 | 日韩欧美三区 | av黄色片在线观看 | 国产精品久久久久久久白浊 | 午夜影院 | 99久久99 | 亚洲成人毛片 | 在线日韩 | 亚洲精品乱码久久久久久9色 | 亚洲激情在线观看 | 国产极品粉嫩美女呻吟在线看人 | 午夜一区二区三区在线观看 | 国产精品中文字幕在线观看 | 天堂久久一区 | 久久久成人免费视频 | 亚洲国产中文字幕 | 看片网站在线 | 亚洲欧美在线一区 | 九九久久精品视频 | 在线欧美视频 | 国产日韩精品一区二区 | 麻豆久久 | 亚洲啊v在线 | 亚洲伊人久久综合 | 九九热这里只有精品在线观看 | 欧美一级淫片免费视频黄 | 欧美亚洲视频 | 国产日韩欧美精品 | 久久国产精品-国产精品 | 一区二区手机在线 |