新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Javadeadlock生成需要使用者注意源代碼
Java deadlock生成需要我們注意的關(guān)鍵點(diǎn)有很多,其實(shí)有不少的問(wèn)題都能在源代碼中尋找到不少的答案。下面我們就看看如何才能更好的做出Java deadlock生成。希望大家有所收獲。

創(chuàng)新互聯(lián)是一家以重慶網(wǎng)站建設(shè)公司、網(wǎng)頁(yè)設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、seo優(yōu)化排名、小程序App開發(fā)等移動(dòng)開發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為成都除甲醛等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
- 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?");
- }
- }
以上就是對(duì)Java deadlock生成的詳細(xì)介紹,希望大家能有所領(lǐng)悟。
【編輯推薦】
- Java多線程中wait語(yǔ)句的具體使用方法
- Java多線程如何防止主線的阻塞
- Java多線程中Message類和Queue類的使用方法
- Java多線程程序如何掌握基本語(yǔ)法
- Java多線程應(yīng)用方法全解密
網(wǎng)頁(yè)名稱:Javadeadlock生成需要使用者注意源代碼
文章來(lái)源:http://www.dlmjj.cn/article/dhcepsi.html


咨詢
建站咨詢
