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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄鸿达 中级黑马   /  2013-2-3 13:20  /  1446 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. public class Test {
  2.         public static void main(String[] args) {
  3.                 Rq r=new Rq();
  4.                 Tq t=new Tq(r);
  5.                 Thread t1=new Thread(t);
  6.                 t1.start();
  7.                 for(int x=0;x<10;x++){
  8.                         System.out.print(x);
  9.                         if(x==1){
  10.                                 try {
  11.                                         t1.join(100);
  12.                                 } catch (InterruptedException e) {
  13.                                         // TODO Auto-generated catch block
  14.                                         e.printStackTrace();
  15.                                 }
  16.                                 if(x==8){
  17.                                         r.notifyAll();
  18.                                 }
  19.                         }
  20.                 }
  21.                
  22.                
  23.         }       
  24.        
  25. }
  26. class Tq implements Runnable{
  27.         private Rq r;
  28.         Tq(Rq r){
  29.                 this.r=r;       
  30.         }
  31.         public void run(){
  32.                 r.show();
  33.         }
  34. }
  35. class Rq{
  36.        
  37.         synchronized void show(){
  38.                 for(int x=0;x<100;x++){
  39.                 System.out.println(Thread.currentThread()+"x="+x);
  40.                 if(x==90){
  41.                         try {
  42.                                 this.wait();
  43.                         } catch (InterruptedException e) {
  44.                                 // TODO Auto-generated catch block
  45.                                 e.printStackTrace();
  46.                         }
  47.                 }
  48.                 }
  49.         }
  50. }
复制代码
我要改什么,才能让show方法内,剩余的x=91到100打印出来?为什么加了r.notifyAll();也没唤醒呢????

1 个回复

倒序浏览
本帖最后由 黄鸿达 于 2013-2-3 14:03 编辑

  if(x==8){
    r.notifyAll();
  }
原来我这个语句放到if(X==1)里了。怪不得之前什么反应都没,你们测试的时候记得改下。
接着如果用T1.INTERRUPUT()就可以打印出后面的91到99,但是还有其他的方法吗?比如用NOTIFY之类的 我用NOTIFY报IllegalMonitorStateException估计是锁的问题,求解决
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马