安卓笔记侠

专注安卓开发

导航

Java产生死锁的一个简单例子

思路是创建两个字符串a和b,再创建两个线程A和B,让每个线程都用synchronized锁住字符串(A先锁a,再去锁b;B先锁b,再锁a),如果A锁住a,B锁住b,A就没办法锁住b,B也没办法锁住a,这时就陷入了死锁。直接贴代码:

 1 public class DeadLock {
 2     public static String obj1 = "obj1";
 3     public static String obj2 = "obj2";
 4     public static void main(String[] args){
 5         Thread a = new Thread(new Lock1());
 6         Thread b = new Thread(new Lock2());
 7         a.start();
 8         b.start();
 9     }    
10 }
11 class Lock1 implements Runnable{
12     @Override
13     public void run(){
14         try{
15             System.out.println("Lock1 running");
16             while(true){
17                 synchronized(DeadLock.obj1){
18                     System.out.println("Lock1 lock obj1");
19                     Thread.sleep(3000);//获取obj1后先等一会儿,让Lock2有足够的时间锁住obj2
20                     synchronized(DeadLock.obj2){
21                         System.out.println("Lock1 lock obj2");
22                     }
23                 }
24             }
25         }catch(Exception e){
26             e.printStackTrace();
27         }
28     }
29 }
30 class Lock2 implements Runnable{
31     @Override
32     public void run(){
33         try{
34             System.out.println("Lock2 running");
35             while(true){
36                 synchronized(DeadLock.obj2){
37                     System.out.println("Lock2 lock obj2");
38                     Thread.sleep(3000);
39                     synchronized(DeadLock.obj1){
40                         System.out.println("Lock2 lock obj1");
41                     }
42                 }
43             }
44         }catch(Exception e){
45             e.printStackTrace();
46         }
47     }
48 }

运行的结果如图所示:

可以看到,Lock1获取obj1,Lock2获取obj2,但是它们都没有办法再获取另外一个obj,因为它们都在等待对方先释放锁,这时就是死锁。

 

如果我们只运行Lock1呢?修改一下main函数,把线程b注释掉。

 1 public class DeadLock {
 2     public static String obj1 = "obj1";
 3     public static String obj2 = "obj2";
 4     public static void main(String[] args){
 5         Thread a = new Thread(new Lock1());
 6         //Thread b = new Thread(new Lock2());
 7         a.start();
 8         //b.start();
 9     }
10 }
11 class Lock1 implements Runnable{
12     @Override
13     public void run(){
14         try{
15             System.out.println("Lock1 running");
16             while(true){
17                 synchronized(DeadLock.obj1){
18                     System.out.println("Lock1 lock obj1");
19                     Thread.sleep(3000);
20                     synchronized(DeadLock.obj2){
21                         System.out.println("Lock1 lock obj2");
22                     }
23                 }
24             }
25         }catch(Exception e){
26             e.printStackTrace();
27         }
28     }
29 }
30 class Lock2 implements Runnable{
31     @Override
32     public void run(){
33         try{
34             System.out.println("Lock2 running");
35             while(true){
36                 synchronized(DeadLock.obj2){
37                     System.out.println("Lock2 lock obj2");
38                     Thread.sleep(3000);
39                     synchronized(DeadLock.obj1){
40                         System.out.println("Lock2 lock obj1");
41                     }
42                 }
43             }
44         }catch(Exception e){
45             e.printStackTrace();
46         }
47     }
48 }

运行结果为:

由于没有其它线程和Lock1争夺obj1和obj2,Lock1可以不断地循环获取并释放它们,这时没有死锁。

posted on 2017-11-11 12:30  安卓笔记侠  阅读(354)  评论(0编辑  收藏  举报