标签:deadlock
1.直接上代码:
class LockDemo{ public static final Object A_LOCK=new Object(); public static final Object B_LOCK=new Object(); } public class ThreadLockDemo implements Runnable{ public boolean flag; public ThreadLockDemo(boolean flag) { this.flag=flag; } public void run() { if(flag) { synchronized (LockDemo.A_LOCK) { System.out.println("if A_LOCK"); synchronized (LockDemo.B_LOCK) { System.out.println("B_LOCK"); } } } else { synchronized (LockDemo.B_LOCK) { System.out.println("else B_LOCK"); synchronized (LockDemo.A_LOCK) { System.out.println("A_LOCK"); } } } } public static void main(String[] args) { // TODO Auto-generated method stub ThreadLockDemo td1=new ThreadLockDemo(false); ThreadLockDemo td2=new ThreadLockDemo(true); Thread t1=new Thread(td1); Thread t2=new Thread(td2); t1.start(); t2.start(); } }
2.有图有真相
标签:deadlock
原文地址:http://blog.csdn.net/mnmlist/article/details/45850183