黑马程序员技术交流社区

标题: Java线程问题 [打印本页]

作者: zhkqy    时间: 2013-12-6 18:36
标题: Java线程问题
本帖最后由 zhkqy 于 2013-12-9 18:30 编辑

有A、B、C三个类(三条线程),让A先执行一秒,然后执行B、C, B、C执行完后,再执行A。我调用wait方法后,不知道在哪里调用notifyAll方法
作者: kongling    时间: 2013-12-7 23:23
实现代码参考:
  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. }
复制代码

作者: Weix1992    时间: 2013-12-7 23:42
我提供一种思路,不保证一定可行,我没有试验。

用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锁上




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2