Java死锁

  1 package pnunu.DeadLock;
  2 
  3 public class DeadLock1 {
  4 
  5     public static void main(String[] args) {
  6         Object o1 = new Object();
  7         Object o2 = new Object();
  8         Object o3 = new Object();
  9         Object o4 = new Object();
 10         Runnable r1 = new TT1(o1, o2);
 11         Runnable r2 = new TT2(o2, o3);
 12         Runnable r3 = new TT3(o3, o4);
 13         Runnable r4 = new TT4(o4, o1);
 14         Thread t1 = new Thread(r1, "r1");
 15         Thread t2 = new Thread(r2, "r2");
 16         Thread t3 = new Thread(r3, "r3");
 17         Thread t4 = new Thread(r4, "r4");
 18         System.out.println("哲学家进餐:");
 19         t1.start();
 20         t2.start();
 21         t3.start();
 22         //解决哲学家进餐问题
 23         /*
 24         try {
 25             Thread.sleep(2000);
 26         } catch (InterruptedException e) {
 27             e.printStackTrace();
 28         }
 29         */
 30         t4.start();
 31     }
 32 }
 33 
 34 class TT1 implements Runnable{
 35 
 36     private Object o1;
 37     private Object o2;
 38     public TT1(Object o1,Object o2){
 39         this.o1 = o1;
 40         this.o2 = o2;
 41     }
 42     @Override
 43     public void run() {
 44         System.out.println(Thread.currentThread()+" 开始");
 45         synchronized (o1) {
 46             try{Thread.sleep(1000);}catch(Exception e){}
 47             synchronized (o2) {
 48                 System.out.println(Thread.currentThread().getName()+"就餐");
 49             }
 50         }
 51         System.out.println(Thread.currentThread()+" 结束");
 52     }
 53     
 54 }
 55 
 56 
 57 class TT2 implements Runnable{
 58 
 59     private Object o1;
 60     private Object o2;
 61     public TT2(Object o1,Object o2){
 62         this.o1 = o1;
 63         this.o2 = o2;
 64     }
 65     @Override
 66     public void run() {
 67         System.out.println(Thread.currentThread()+" 开始");
 68         synchronized (o1) {
 69             try{Thread.sleep(1000);}catch(Exception e){}
 70             synchronized (o2) {
 71                 System.out.println(Thread.currentThread().getName()+"就餐");
 72             }
 73         }
 74         System.out.println(Thread.currentThread()+" 结束");
 75     }
 76 }
 77 
 78 
 79 class TT3 implements Runnable{
 80 
 81     private Object o1;
 82     private Object o2;
 83     public TT3(Object o1,Object o2){
 84         this.o1 = o1;
 85         this.o2 = o2;
 86     }
 87     @Override
 88     public void run() {
 89         System.out.println(Thread.currentThread()+" 开始");
 90         synchronized (o1) {
 91             try{Thread.sleep(1000);}catch(Exception e){}
 92             synchronized (o2) {
 93                 System.out.println(Thread.currentThread().getName()+"就餐");
 94             }
 95         }
 96         System.out.println(Thread.currentThread()+" 结束");
 97     }
 98 }
 99 
100 class TT4 implements Runnable{
101 
102     private Object o1;
103     private Object o2;
104     public TT4(Object o1,Object o2){
105         this.o1 = o1;
106         this.o2 = o2;
107     }
108     @Override
109     public void run() {
110         System.out.println(Thread.currentThread()+" 开始");
111         synchronized (o1) {
112             try{Thread.sleep(1000);}catch(Exception e){}
113             synchronized (o2) {
114                 System.out.println(Thread.currentThread().getName()+"就餐");
115             }
116         }
117         System.out.println(Thread.currentThread()+" 结束");
118     }
119 }

运行截图:

原文地址:https://www.cnblogs.com/acmwangpeng/p/5801588.html