- <p>class LockTest implements Runnable
- {
- private boolean flag;
- LockTest(boolean flag)
- {
- this.flag=flag;
- }
- public void run()
- {
- if(flag)
- {
- while(true)
- {
- synchronized(LockClass.locka)
- {
- System.out.println(Thread.currentThread().getName()+"---if_locka");
- }
- synchronized(LockClass.lockb)
- {
- System.out.println(Thread.currentThread().getName()+"----if_lockb");
- }
-
- }
- }
- else
- {
- while(true)
- {
- synchronized(LockClass.lockb)
- {
- System.out.println(Thread.currentThread().getName()+"----else_lockb");
- }
- synchronized(LockClass.locka)
- {
- System.out.println(Thread.currentThread().getName()+"---else_locka");
- }
- }
- }
- }
- }
- class LockClass
- {
- static Object locka = new Object();
- static Object lockb = new Object();
- }</p><p>class DeadLock
- {
- public static void main(String[] args)
- {
- new Thread(new LockTest(true)).start();
- new Thread(new LockTest(false)).start();
- }
- }
- <img border="0" alt="" src="http://bbs.itheima.com/forum.php?mod=image&aid=62462&size=300x300&key=485d320d39dce60f&nocache=yes&type=fixnone" aid="attachimg_62462"></p><p> </p>
复制代码 来大神帮我看看这算是死锁吗??为什么我运行的是无限循环啊,到底哪里出错了。
|
|