ThreadDeath 理解

 1 public class RunnableTest2 {
 2     public static Object obj1 = new Object();
 3     public static Object obj2 = new Object();
 4     private MyThread1 t1 = new MyThread1();
 5     private MyThread2 t2 = new MyThread2();
 6     public static void main(String[] args) {
 7         RunnableTest2 tr = new RunnableTest2();
 8         tr.t1.start();
 9         tr.t2.start();
10     }
11 }
12 
13 class MyThread1 extends Thread {
14     @Override
15     public void run() {
16         System.out.println(Thread.currentThread()+"锁定了obj1,过2秒锁定obj2去");
17         synchronized (RunnableTest2.obj1) {
18             try {
19                 Thread.sleep(2000);
20                 synchronized (RunnableTest2.obj2) {
21                     System.out.println("1");
22                 }
23             } catch (InterruptedException e) {
24                 e.printStackTrace();
25             }
26         }
27     }
28 }
29 
30 class MyThread2 extends Thread {
31     @Override
32     public void run() {
33             System.out.println(Thread.currentThread()+"锁定了obj2,过2秒锁定obj1去");
34             synchronized (RunnableTest2.obj2) {
35                 
36                 try {
37                     Thread.sleep(2000);
38                     synchronized (RunnableTest2.obj1) {
39                         System.out.println("2");
40                     }
41                 } catch (InterruptedException e) {
42                     // TODO Auto-generated catch block
43                     e.printStackTrace();
44                 }
45                 
46                 
47             }
48     }
49 }

打印结果:

Thread[Thread-0,5,main]锁定了obj1,过2秒锁定obj2去
Thread[Thread-1,5,main]锁定了obj2,过2秒锁定obj1去

 

posted on 2015-11-22 16:26  freedom's_blog  阅读(1927)  评论(0编辑  收藏  举报

导航