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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zhkqy 中级黑马   /  2013-12-6 18:36  /  1023 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 zhkqy 于 2013-12-9 18:30 编辑

有A、B、C三个类(三条线程),让A先执行一秒,然后执行B、C, B、C执行完后,再执行A。我调用wait方法后,不知道在哪里调用notifyAll方法

评分

参与人数 1技术分 +1 收起 理由
贺奕凯 + 1

查看全部评分

2 个回复

倒序浏览
实现代码参考:
  1. package biji;

  2. public class Test4
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 // TODO Auto-generated method stub
  7.                
  8.                 final ThreadTest test=new ThreadTest();
  9.                 new Thread(new Runnable()
  10.                 {

  11.                         @Override
  12.                         public void run()
  13.                         {
  14.                                 while(true)
  15.                                 {
  16.                                         test.A();
  17.                                         // TODO Auto-generated method stub
  18.                                 }
  19.                         }
  20.                 }).start();;
  21.                        
  22.                 new Thread(new Runnable()
  23.                 {
  24.                         @Override
  25.                         public void run()
  26.                         {
  27.                                 while(true)
  28.                                 {
  29.                                         test.B();
  30.                                         // TODO Auto-generated method stub
  31.                                 }       
  32.                         }
  33.                 }).start();;       
  34.                                
  35.                 new Thread(new Runnable()
  36.                 {
  37.                                 @Override
  38.                                 public void run()
  39.                                 {
  40.                                         while(true)
  41.                                         {
  42.                                                 test.C();
  43.                                                 // TODO Auto-generated method stub
  44.                                         }       
  45.                                 }
  46.                 }).start();;               
  47.         }

  48. }

  49. class ThreadTest
  50. {
  51.         boolean tagB=true;
  52.         boolean tagC=true;
  53.         public synchronized void A()
  54.         {
  55.                 while((tagB && tagC)==false)
  56.                 {
  57.                         try
  58.                         {
  59.                                 this.wait();
  60.                         }
  61.                         catch (InterruptedException e)
  62.                         {
  63.                                 // TODO Auto-generated catch block
  64.                                 e.printStackTrace();
  65.                         }
  66.                 }
  67.                
  68.                 try
  69.                 {
  70.                         System.out.println("A finish");
  71.                         Thread.sleep(1000);
  72.                 }
  73.                 catch (InterruptedException e)
  74.                 {
  75.                         // TODO Auto-generated catch block
  76.                         e.printStackTrace();
  77.                 }               
  78.                 tagB=false;
  79.                 tagC=false;
  80.                 this.notifyAll();       
  81.         }
  82.        
  83.         public synchronized void B()
  84.         {
  85.                 while(tagB==true)
  86.                 {
  87.                         try
  88.                         {
  89.                                 this.wait();
  90.                         }
  91.                         catch (InterruptedException e)
  92.                         {
  93.                                 // TODO Auto-generated catch block
  94.                                 e.printStackTrace();
  95.                         }
  96.                 }
  97.                
  98.                 System.out.println("B finish");
  99.                 tagB=true;
  100.                 this.notifyAll();
  101.         }
  102.         public  synchronized void C()
  103.         {
  104.                 while(tagC==true)
  105.                 {
  106.                         try
  107.                         {
  108.                                 this.wait();
  109.                         }
  110.                         catch (InterruptedException e)
  111.                         {
  112.                                 // TODO Auto-generated catch block
  113.                                 e.printStackTrace();
  114.                         }
  115.                 }
  116.                 System.out.println("C finish");
  117.                 tagC=true;
  118.                 this.notifyAll();
  119.         }
  120. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
我提供一种思路,不保证一定可行,我没有试验。

用1.5的新特性,  Condition a = lock.newCondition();
Condition b = lock.newCondition();
Condition c = lock.newCondition();

用3个Condition 分别对应三个线程, B,C线程启动的时候都是await()的,然后a控制b,b控制c,c控制a每个线程用lock锁上

评分

参与人数 1黑马币 +3 收起 理由
简★零度 + 3

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马