黑马程序员技术交流社区

标题: 多线程等待唤醒机制的代码优化 [打印本页]

作者: More    时间: 2014-7-13 06:43
标题: 多线程等待唤醒机制的代码优化
昨天看了基础视频的等待唤醒机制的代码优化  为什么会出现编译错误  我调了好长时间调不出来
  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.                
复制代码

作者: 劇情侢媄終究媞    时间: 2014-7-13 08:01
  1.         while (true) {
  2.                         if (x == 0) {
  3.                                 r.set("mike", "man");
  4.                                 r.set("丽丽", "女女女女");
  5.                                 x = (x + 1) % 2;
  6.                         }
复制代码
问题代码处在这里.


作者: 劇情侢媄終究媞    时间: 2014-7-13 08:02
  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.         }
复制代码



作者: More    时间: 2014-7-13 10:41
劇情侢媄終究媞 发表于 2014-7-13 08:01
问题代码处在这里.

这里有错吗  你不改变x的值怎么进行if判断呢??
作者: OCTSJimmy    时间: 2014-7-13 10:53
if(x==0){//注意:这个大括号没有闭合
                                r.set("mike","man");
作者: More    时间: 2014-7-13 13:00
OCTSJimmy 发表于 2014-7-13 10:53
if(x==0){//注意:这个大括号没有闭合
                                r.set("mike","man"); ...

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




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