Java deadlock生成的示例分析,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)岫巖,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
Java deadlock生成需要我們注意的關(guān)鍵點有很多,其實有不少的問題都能在源代碼中尋找到不少的答案。下面我們就看看如何才能更好的做出Java deadlock生成。
public class Deadlock extends Object ...{ private String objID; public Deadlock(String id) ...{ objID = id; } public synchronized void checkOther(Deadlock other) ...{ print("entering checkOther()"); // simulate some lengthy process try ...{ Thread.sleep(2000); } catch (InterruptedException x) ...{ } print("in checkOther() - about to " + "invoke 'other.action()'"); other.action(); print("leaving checkOther()"); } public synchronized void action() ...{ print("entering action()"); // simulate some work here try ...{ Thread.sleep(500); } catch (InterruptedException x) ...{ } print("leaving action()"); } public void print(String msg) ...{ threadPrint("objID=" + objID + " - " + msg); } public static void threadPrint(String msg) ...{ String threadName = Thread.currentThread().getName(); System.out.println(threadName + ": " + msg); } public static void main(String[] args) ...{ final Deadlock obj1 = new Deadlock("obj1"); final Deadlock obj2 = new Deadlock("obj2"); Runnable runA = new Runnable() ...{ public void run() ...{ obj1.checkOther(obj2); } }; Thread threadA = new Thread(runA, "threadA"); threadA.start(); try ...{ Thread.sleep(200); } catch (InterruptedException x) ...{ } Runnable runB = new Runnable() ...{ public void run() ...{ obj2.checkOther(obj1); } }; Thread threadB = new Thread(runB, "threadB"); threadB.start(); try ...{ Thread.sleep(5000); } catch (InterruptedException x) ...{ } threadPrint("finished sleeping"); threadPrint("about to interrupt() threadA"); threadA.interrupt(); try ...{ Thread.sleep(1000); } catch (InterruptedException x) ...{ } threadPrint("about to interrupt() threadB"); threadB.interrupt(); try ...{ Thread.sleep(1000); } catch (InterruptedException x) ...{ } threadPrint("did that break the deadlock?"); } }
以上就是對Java deadlock生成的詳細(xì)介紹,希望大家能有所領(lǐng)悟。
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
網(wǎng)站名稱:Javadeadlock生成的示例分析
標(biāo)題URL:http://jinyejixie.com/article48/ggggep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)站設(shè)計、響應(yīng)式網(wǎng)站、標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)