package jiben; public class DeadLock { volatile int ff = 0; aa a = null; public void setA(aa aa){ this.a = aa; } public static void main(String[] args) { // TODO Auto-generated method stub final DeadLock one = new DeadLock(); final aa two = new aa(); one.setA(two); two.setD(one); for(int i=0;i<2;i++){ new Thread(new Runnable() { public void run() { System.out.println(Thread.currentThread().getName() + "---run"); if(one.ff==0){ one.ff = 1; one.do1(); two.do2(); }else{ two.do2(); one.do1(); one.ff = 0; } } }).start();; } } public synchronized void do1(){ System.out.println(Thread.currentThread().getName() + "----1111start"); a.do2(); System.out.println(Thread.currentThread().getName() + "----1111end"); } } class aa{ DeadLock d =null; public void setD(DeadLock dd){ this.d = dd; } public synchronized void do2(){ System.out.println(Thread.currentThread().getName() + "----222start"); d.do1(); System.out.println(Thread.currentThread().getName() + "----222end"); } }
java死锁代码
最新推荐文章于 2024-06-25 19:58:37 发布