本帖最后由 王涛 于 2012-6-25 16:17 编辑
- class Zhangsan
- {
- public void say()
- {
- System.out.println("张三对李四说:“你给我画,我就把书给你”");
- }
- public void get()
- {
- System.out.println("张三得到画了");
- }
- }
- class Lisi
- {
- public void say()
- {
- System.out.println("李四对张三说:“你给我书,我就把画给你”");
- }
- public void get()
- {
- System.out.println("李四得到书了");
- }
- }
- public class ThreadDeadLock implements Runnable
- {
- private static Zhangsan zs = new Zhangsan();
- private static Lisi ls = new Lisi();
- private boolean flag = false;
- public void run()
- {
- if(flag)
- {
- synchronized(zs)
- {
- zs.say();
- try
- {
- Thread.sleep(500);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- synchronized(ls)
- {
- zs.get();
- }
- }
- }else
- {
- synchronized(ls)
- {
- ls.say();
- try
- {
- Thread.sleep(500);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- synchronized(ls)
- {
- zs.get();
- }
- }
- }
- }
- public static void main(String[] args)
- {
- ThreadDeadLock t1 = new ThreadDeadLock();
- ThreadDeadLock t2 = new ThreadDeadLock();
- t1.flag = true;
- t2.flag = false;
- Thread thA = new Thread(t1);
- Thread thB = new Thread(t2);
- thA.start();
- thB.start();
- }
- }
这个是那个死锁问题,输出应该是:
张三对李四说:“你给我画,我就把书给你”
李四对张三说:“你给我书,我就把画给你”
可是为什么一直没有出现死锁啊,输出的是:
张三对李四说:“你给我画,我就把书给你”
李四对张三说:“你给我书,我就把画给你”
张三得到画了
李四得到书了
谁能看看哪里出错了吗?谢谢了。 |