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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java - 多線程死鎖測(cè)試

瀏覽:107日期: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先運(yùn)行一秒TimeUnit.SECONDS.sleep(1);t2.start(); }}

如上,隨便找的產(chǎn)生死鎖的代碼,問題:TimeUnit.SECONDS.sleep(1);加上這行后,不存在死鎖問題。sleep并不釋放鎖,為何這邊死鎖情況會(huì)消失。輸出結(jié)果為: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

注掉這行,正常死鎖。輸出結(jié)果為: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的鎖.此時(shí)但是他們同時(shí)都想要獲取other的鎖 此時(shí)誰(shuí)也不讓誰(shuí) 發(fā)生死鎖解決方法讓倆個(gè)線程不要同時(shí)去搶第二把鎖.讓A停一會(huì)但是如果你把時(shí)間調(diào)成納秒級(jí)別 多次嘗試也會(huì)發(fā)生死鎖不建議這樣預(yù)防死鎖.如果并發(fā)量高的情況下.

回答2:

雙方在爭(zhēng)同一把鎖,不會(huì)死鎖啊

回答3:

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

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

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 亚洲国产精品日韩高清秒播 | 久草中文在线 | 美女被免费视频网站a国产 美女被免费网站视频软件 美女被免费网站在线软件 美女被免费网站在线视频软件 | 日韩在线观看一区二区三区 | 在线免费观看日本视频 | 精品免费国产一区二区三区 | 窝窝女人体国产午夜视频 | 91精品国产高清91久久久久久 | 欧美一级久久久久久久大 | 国产精品怡红院在线观看 | 精品午夜国产在线观看不卡 | 99久久久免费精品免费 | 三级c欧美做人爱视频 | 久久一日本道色综合久久m 久久伊人成人网 | 久久亚洲成a人片 | 成人手机看片 | 亚洲精品资源网在线观看 | 一级做a爰片性色毛片视频图片 | 欧美一级片在线视频 | 美国特级毛片 | 最全精品自拍视频在线 | 免费一区二区三区视频狠狠 | 亚洲一区在线免费观看 | 国产真实乱系列2孕妇 | 免费一区二区 | 久久熟| 草草国产成人免费视频 | 久久亚洲精品中文字幕二区 | 中国老太卖淫播放毛片 | 色片在线播放 | 国产在线精品福利91香蕉 | 欧美成人中文字幕 | 台湾一级特黄精品大片 | 国产手机在线国内精品 | 一级做a爱久久久久久久 | 欧美一级视频免费观看 | 亚洲午夜a | 国产91精品露脸国语对白 | 亚洲精品欧美 | 全免费a级毛片免费看 | 在线播放一区二区精品产 |