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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© More 中级黑马   /  2014-7-13 06:43  /  1159 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

昨天看了基础视频的等待唤醒机制的代码优化  为什么会出现编译错误  我调了好长时间调不出来
  1. class Res
  2. {
  3.         private String name;
  4.         private String sex;
  5.         boolean flag=false;
  6.         public synchronized void set(String name,String sex){
  7.                 if(flag)
  8.                         try{this.wait();}catch(Exception e){}
  9.                 this.name=name;
  10.                 this.sex=sex;
  11.                 flag=true;
  12.                 this.notify();
  13.         }
  14.         public synchronized void out(){
  15.                 if(!flag)
  16.                         try{this.wait();}catch(Exception e){}
  17.                 System.out.println(name+"...."+sex);
  18.                 flag=false;
  19.                 this.notify();
  20.         }
  21.        
  22. }
  23. class Input implements Runnable
  24. {
  25.         private Res r;
  26.         Input(Res r){
  27.                 this.r=r;       
  28.         }
  29.         public void run(){
  30.                 int x=0;
  31.                 while (true)
  32.                 {       
  33.                         if(x==0){
  34.                                 r.set("mike","man");
  35.                         r.set("丽丽","女女女女");
  36.                         x=(x+1)%2;
  37.                 }
  38.         }
  39. }
  40. class Output implements Runnable
  41. {
  42.         private Res r;
  43.        
  44.         Output(Res r){
  45.                 this.r=r;       
  46.         }
  47.         public void run(){
  48.                 while(true){
  49.                         r.out();       
  50.                 }
  51.         }
  52. }
  53. class InputOutputDemo2
  54. {
  55.         public static void main(String []args){
  56.                 Res r=new Res();
  57.                 new Thread(new Input(r)).start();
  58.                 new Thread(new Output(r)).start();

  59.         }
  60. }
  61.                
复制代码

6 个回复

倒序浏览
  1.         while (true) {
  2.                         if (x == 0) {
  3.                                 r.set("mike", "man");
  4.                                 r.set("丽丽", "女女女女");
  5.                                 x = (x + 1) % 2;
  6.                         }
复制代码
问题代码处在这里.

回复 使用道具 举报
  1. while(true)
  2.                 {
  3.                         //lock.lock();//获得锁
  4.                        
  5.                         synchronized (r) {
  6.                                
  7.                                 if (r.fals) {
  8.                                        
  9.                                         try {
  10.                                                 r.wait();
  11.                                         } catch (InterruptedException e) {
  12.                                                 // TODO Auto-generated catch block
  13.                                                 e.printStackTrace();
  14.                                         }
  15.                                 }
  16.                                
  17.                                 if (x==0) {
  18.                                         r.name="张飞";
  19.                                         r.sex="男";
  20.                                 }else {
  21.                                         r.name="rose";
  22.                                         r.sex="女女女";
  23.                                 }
  24.                                        
  25.                        
  26.                                
  27.                                 r.fals=true;
  28.                                 r.notify();
  29.                                
  30.                         }
  31.                        
  32.                         x=(x+1)%2;//实现切换.
  33.                         //lock.unlock();//释放锁
  34.                 }
  35.         }
复制代码


回复 使用道具 举报

这里有错吗  你不改变x的值怎么进行if判断呢??
回复 使用道具 举报
if(x==0){//注意:这个大括号没有闭合
                                r.set("mike","man");
回复 使用道具 举报
OCTSJimmy 发表于 2014-7-13 10:53
if(x==0){//注意:这个大括号没有闭合
                                r.set("mike","man"); ...

十分感谢  查错的时候有点不耐烦  谢谢

点评

不客气的!  发表于 2014-7-13 13:01
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马