- class Lock2
- {
- static Object obj = new Object();
- static Object obj1 = new Object();
- }
- class Demo extends Thread
- {
- public void run()
- {
- while(true)
- {
- synchronized(Lock2.obj)
- {
- System.out.println("============zhangsan");
- synchronized(Lock2.obj1)
- {
- System.out.println("=====lisi");
- }
- }
- }
- }
- }
- class Demo1 extends Thread
- {
- public void run()
- {
- while(true)
- {
- synchronized(Lock2.obj1)
- {
- System.out.println("============zhangsan===");
- synchronized(Lock2.obj)
- {
- System.out.println("=====lisi===");
- }
- }
- }
- }
- }
- class Lock1
- {
- public static void main(String[] args)
- { Demo1 d = new Demo1();
- d.start();
- Demo d1 = new Demo();
- d1.start();
-
- }
- }
复制代码
对于初学者的我感觉 在 别的 类中定锁来过来用 这样的话 教好理解一点 希望对初学者有点用 |
|