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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 。。。。。。。 中级黑马   /  2013-12-4 15:04  /  1134 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

多线程中,线程wait方法抛出异常后,就释放了锁进入了临时堵塞状态,当该线程被唤醒以后,为什么还要去判断while语句的参数呢?wait方法不是在while语句内吗,应该是唤醒后继续往下运行撒

评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
狼王 + 1 + 3 很给力!

查看全部评分

4 个回复

倒序浏览
重点在于:被唤醒以后为什么还会去判断while语句条件,先不是已经判断过了吗?难道不是应该继续向下执行吗?
回复 使用道具 举报
求高人指点,在线坐等!
回复 使用道具 举报
本帖最后由 天下 于 2013-12-4 15:49 编辑

哥们,不知道你是指的哪一个例子;
比如:生产者消费者那个例子,循环判断是为了保证不产生数据错乱,

         
  1. //如下定义了一个产品类,产品类有自己的属性,然后创建了两个Runnable线
  2. //程类,一个为生产者Producer和消费者Consumer,这两个类持有了对产品类
  3. //的引用,可以方法和操作产品类对象的属性,达到生产者在刷新产品的属性,//模仿了产品生产的过程,而消费者便可以取得产品的信息,模仿了产
  4. //品被消费的过程。
  5. packagecom.thread;
  6. public classProducerAndConsumer
  7. {
  8.        public static void main(String[] args)
  9.        {
  10.               Product p = new Product();
  11.         Producer pr = new Producer(p);
  12.         Consumer cr = new Consumer(p);
  13.         Thread t1 = new Thread(pr);
  14.         Thread t2 = new Thread(pr);
  15.         Thread t3 = new Thread(cr);
  16.         Thread t4 = new Thread(cr);
  17.         t1.start();
  18.         t2.start();
  19.         t3.start();
  20.         t4.start();
  21.        }
  22. }
  23. //定一个产品类
  24. class Product
  25. {
  26.         private String name;
  27.         private int ID = 1;
  28.         public boolean flag = false;
  29.      public synchronized void  setProperty(String name)
  30.         {
  31.                                     while(flag)
  32.                                    {
  33.                                                 try
  34.                                                 {
  35.                                                        this.wait();
  36.                                                 }
  37.                                                     catch(Exception e)
  38.                                                 {
  39.                                                 };
  40.                                    }
  41. this.name =name+" "+ID++;
  42. System.out.println(Thread.currentThread().getName()+" 生产出 "+this.name);
  43.                                                         flag = true;
  44.                                                         this.notifyAll();
  45.         }
  46.      public synchronized void getInfo()
  47.      {
  48. //这里使用Wlile而不是if实用为while会条件会被判断两次,不会到一个产品被//消费两次,或程序挂起的状况。
  49.                         while(!flag)
  50.                         {
  51.                                                try
  52.                                                {
  53.                                                       this.wait();
  54.                                                }
  55.                                                    catch(Exception e)
  56.                                                {
  57.                                                };
  58.                         }
  59. System.out.println(Thread.currentThread().getName()+" 消费了 "+this.name+" o(∩_∩)o...哈哈!!!");
  60.                                         flag = false;
  61. //这里notifyAll()是为了防止之唤醒本对列的线程,却没有唤醒对方的线程,最
  62. //终导致程序都进入wait状态,程序挂起。
  63.                                         this.notifyAll();
  64.      }
  65. }

  66. class Producer implements Runnable
  67. {
  68.           private Product p;
  69.           public Producer(Product p)
  70.           {
  71.                  this.p = p;
  72.           }
  73.           @Override
  74.           public void run()
  75.           {
  76.                 while(true)
  77.                 {
  78.                         p.setProperty("糖果");
  79.                 }
  80.               
  81.           }
  82. }

  83. class Consumer implements Runnable
  84. {
  85.          private Product p ;
  86.          publicConsumer(Product p)
  87.          {
  88.                this.p = p;
  89.          }
  90.          @Override
  91.        public void run()
  92.        {
  93.                while(true)
  94.                {
  95.                                    p.getInfo();
  96.                }
  97.        }
  98. }
复制代码


评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
狼王 + 1 + 3 赞一个!

查看全部评分

回复 使用道具 举报
你可以将wait语句看成普通语句,它在while语句中,当它运行完了的时候当然要去while内进行语句判断呀!
希望对你有所帮助!!!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马