日期:2014-05-20 浏览次数:20709 次
class test implements Runnable{ private boolean flag; public test(boolean flag){ this.flag=flag; } public void run(){ if(flag){ synchronized (Mylock.locka) { System.out.println("if locka"); synchronized (Mylock.lockb) { System.out.println("if lockb"); } } }else{ synchronized (Mylock.lockb) { System.out.println("else lockb"); synchronized (Mylock.locka) { System.out.println("else locka"); } } } } } class Mylock{ static Object locka=new Object(); static Object lockb=new Object(); } public class DeadLockTest{ public static void main(){ Thread t1=new Thread(new test(true)); Thread t2=new Thread(new test(false)); t1.start(); t2.start(); } }