java制造死锁

来源:互联网 发布:mac射手影音字幕位置 编辑:程序博客网 时间:2024/04/29 20:56
package suo;public class DeadLockTest implements Runnable{ private int flag; static Object o1 = new Object(), o2 = new Object();      //静态的对象,被DeadLockTest的所有实例对象所公用 public void run(){  System.out.println(flag);  if(flag == 0){   synchronized(o1){    try{     Thread.sleep(500);    } catch(Exception e){     e.printStackTrace();    }    synchronized(o2){    }   }   }  if(flag == 1){   synchronized(o2){    try{     Thread.sleep(500);    } catch(Exception e){     e.printStackTrace();    }    synchronized(o1){    }   }   }  } public static void main(String[] args){  DeadLockTest test1 = new DeadLockTest();  DeadLockTest test2 = new DeadLockTest();  test1.flag = 1;  test2.flag = 0;  Thread thread1 = new Thread(test1);  Thread thread2 = new Thread(test2);  thread1.start();  thread2.start(); }}/*解释:在main方法中,实例化了两个实现了Runnable接口的DeadLockTest对象test1和test2,test1的flag等于1,所以在thread1线程执行的时候执行的是run()方法后半部分的代码,test2的flag等于2,所以在thread2线程启动的时候执行的是run()方法前半部分的代码,此时,出现了下列现象:thread1线程占有了o1对象并等待o2对象,而thread2线程占有了o2对象并等待o1对象,而o1和o2又被这俩个线程所共享,所以就出现了死锁的问题了。*/

0 0