代码如下- class Test implements Runnable
- {
- private boolean flag;
- Test(boolean flag)
- {
- this.flag=flag;
- }
- public void run()
- {
- if(flag)
- {
- synchronized(Mylock.lockaa)
- {
- System.out.println("if lockaa");
- synchronized(Mylock.lockbb)
- {
- System.out.println("if lockbb");
- }
- }
- }
- else
- {
- synchronized(Mylock.lockbb)
- {
- System.out.println("else lockbb");
- synchronized(Mylock.lockaa)
- {
- System.out.println("else lockaa");
- }
- }
- }
- }
- }
- class Mylock
- {
- static Object lockaa=new Object();
- static Object lockbb=new Object();
- }
- class Lock
- {
- public static void main(String[] args)
- {
- Test t1=new Test(true);
- Test t2=new Test(false);
- Thread t11=new Thread(t1);
- Thread t22=new Thread(t2);
- t11.start();
- t22.start();
- }
- }
复制代码 运行结果
|
|