java死锁代码

来源:互联网 发布:网络用语1是什么意思 编辑:程序博客网 时间:2024/05/16 10:02
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 stubfinal 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");}}
 
原创粉丝点击