Java並發之死結執行個體,java之死執行個體

來源:互聯網
上載者:User

Java並發之死結執行個體,java之死執行個體

 1 package com.thread.test.thread; 2  3 /** 4  * Created by windwant on 2016/6/3. 5  */ 6 public class MyTestDeadLock { 7     public void run() { 8         MyThread mt = new MyThread(); 9         new Thread(mt, "zhangsan").start();10         new Thread(mt, "lisi").start();11     }12 13     class MyThread implements Runnable {14         private Object o1 = new Object();15         private Object o2 = new Object();16         private boolean flag = true;17 18 19         public void run() {20             if (flag) {21                 flag = false;22                 synchronized (o1) {23                     System.out.println(Thread.currentThread().getName() + " have o1");24                     try {25                         Thread.sleep(100);26                     } catch (InterruptedException e) {27                         e.printStackTrace();28                     }29                     synchronized (o2) {30                         System.out.println(Thread.currentThread().getName() + " have o2");31                     }32                 }33             } else {34                 flag = true;35                 synchronized (o2) {36                     System.out.println(Thread.currentThread().getName() + " have o2");37                     try {38                         Thread.sleep(100);39                     } catch (InterruptedException e) {40                         e.printStackTrace();41                     }42                     synchronized (o1) {43                         System.out.println(Thread.currentThread().getName() + " have o1");44                     }45                 }46             }47         }48     }49 50     public static void main(String[] args) {51         new MyTestDeadLock().run();52     }53 }

項目地址:https://github.com/windwant/threadtest

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.