A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© MilesMatheson 中级黑马   /  2015-9-6 23:31  /  393 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package threadDemos;

  2. class DeadLockRunnable implements  Runnable
  3. {
  4.         static Object chopsticks = new Object();
  5.         static Object knifeAndFork = new Object();
  6.        
  7.         private boolean flag;
  8.         DeadLockRunnable (boolean flag)
  9.         {
  10.                 this.flag = flag;
  11.         }
  12.         @Override
  13.         public void run() {
  14.                 if(flag)
  15.                 {
  16.                         while(true)
  17.                         {
  18.                                
  19.                                 synchronized (chopsticks) {
  20.                                
  21.                                         System.out.println(Thread.currentThread().
  22.                                                         getName()+"-------Give me the knife!");
  23.                                        
  24.                                 try {
  25.                                         Thread.sleep(500);
  26.                                 } catch (InterruptedException e) {
  27.                                         // TODO Auto-generated catch block
  28.                                         e.printStackTrace();
  29.                                 }
  30.                                
  31.                                 }
  32.                                 synchronized (knifeAndFork) {
  33.                                
  34.                                         System.out.println(Thread.currentThread().
  35.                                                         getName()+"-------Give me the chopsticks!");
  36.                                         try {
  37.                                                 Thread.sleep(500);
  38.                                         } catch (InterruptedException e) {
  39.                                                 // TODO Auto-generated catch block
  40.                                                 e.printStackTrace();
  41.                                         }
  42.                                        
  43.                                 }
  44.                         }
  45.                 }
  46.                 else
  47.                 {
  48.                         while(true)
  49.                         {
  50.                                 synchronized (knifeAndFork) {
  51.                                        
  52.                                         System.out.println(Thread.currentThread().
  53.                                                         getName()+"-------Give me the chopsticks!");
  54.                                         try {
  55.                                                 Thread.sleep(500);
  56.                                         } catch (InterruptedException e) {
  57.                                                 // TODO Auto-generated catch block
  58.                                                 e.printStackTrace();
  59.                                         }       
  60.                                        
  61.                                 }
  62.                                 synchronized (chopsticks) {
  63.                                
  64.                                         System.out.println(Thread.currentThread().
  65.                                                         getName()+"-------Give me the knife!");
  66.                                         try {
  67.                                                 Thread.sleep(500);
  68.                                         } catch (InterruptedException e) {
  69.                                                 // TODO Auto-generated catch block
  70.                                                 e.printStackTrace();
  71.                                         }
  72.                                        
  73.                                 }
  74.                         }
  75.                 }
  76.                
  77.         }
  78.        
  79.        
  80.        
  81. }
  82. public class DieLockDemo {

  83.         /**
  84.          * @param args
  85.          */
  86.         public static void main(String[] args) {
  87.                 //创建两个DeadLockRunnable对象
  88.                 DeadLockRunnable dlr = new DeadLockRunnable(true);
  89.                 DeadLockRunnable dlr2 = new DeadLockRunnable(false);
  90.                 //创建并开启两个线程
  91.                 new Thread(dlr,"Chinese").start();
  92.                 new Thread(dlr2,"America").start();
  93.                

  94.         }

  95. }
复制代码


困扰我好久,都不知道这是不是死锁,怎么看出来是不是死锁呢?求赐教,
部分执行结果如下:
Chinese-------Give me the knife!
America-------Give me the chopsticks!
America-------Give me the knife!
Chinese-------Give me the chopsticks!
Chinese-------Give me the knife!
America-------Give me the chopsticks!
America-------Give me the knife!
Chinese-------Give me the chopsticks!
America-------Give me the chopsticks!
Chinese-------Give me the knife!
Chinese-------Give me the chopsticks!
America-------Give me the knife!
America-------Give me the chopsticks!
Chinese-------Give me the knife!
America-------Give me the knife!
Chinese-------Give me the chopsticks!

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马