本帖最后由 ka宾 于 2013-12-1 16:38 编辑
- class DeadLock
- {
- public static void main(String[] args)
- {
- new Thread(new DeadLockTest(false)).start();
- new Thread(new DeadLockTest(true)).start();
- }
- }
- class MyLock
- {
- static Object lock_a = new Object();
- static Object lock_b = new Object();
- }
- class DeadLockTest implements Runnable
- {
- private int x,y;
- static Boolean boo;
- DeadLockTest(Boolean value)
- {
- boo = value;
- }
- public void run()
- {
- if(boo)
- {
- while(true)
- {
- x++;
- synchronized(MyLock.lock_a)
- {
- System.out.println("线程_1...lock_a..."+x);
- synchronized(MyLock.lock_b)
- {
- System.out.println("线程_1...lock_b..."+x);
- }
- }
- }
- }else
- {
- while(true)
- {
- y++;
- synchronized(MyLock.lock_b)
- {
- System.out.println("线程_2...lock_b..."+y);
- synchronized(MyLock.lock_a)
- {
- System.out.println("线程_2...lock_a..."+y);
- }
- }
- }
- }
- }
- }
复制代码 运行结果是 线程1 无限地运行...怎么没有 线程2 呢?{:soso_e103:} 先谢谢各位老师了~!
-------------------------------------------------------------------
有人提醒我了,原来boo值写了static 。。。。我自己都觉得奇怪{:soso_e153:}
|