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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© JACK...... 中级黑马   /  2013-12-17 10:55  /  988 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 JACK...... 于 2013-12-17 11:17 编辑

复制代码
  1. class Res
  2. {
  3.         private String name;
  4.         private String sex;
  5.         private 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. class Input implements Runnable
  23. {
  24.         //资源对象
  25.         private Res r;
  26.         //构造函数,传进来一个对象,将引用赋给r
  27.         Input(Res r)
  28.         {
  29.                 this.r=r;
  30.         }
  31.         public void run()
  32.         {
  33.                 int x=0;
  34.                 while(true)
  35.                 {
  36.                         if(x==0)
  37.                                         r.set("mike","man");
  38.                   
  39.                   else
  40.                                         r.set("丽丽","女");
  41.                        
  42.                         x=(x+1)%2;
  43.                 }
  44.                
  45.         }
  46.        
  47. }
  48. class Output implements Runnable
  49. {
  50.                         private Res r;
  51.                         Output(Res r){
  52.                                 this.r=r;
  53.                         }
  54.                         public void run(){
  55.                                 while(true){
  56.                                         r.out();
  57.                                 }
  58.                        
  59.                         }
  60.        
  61. }
  62. class Duoxc
  63. {
  64.         public static void main(String[] args){
  65.                 Res r=new Res();
  66.                 new Thread(new Input(r)).start();
  67.                 new Thread(new Output(r)).start();
  68.         }
  69. }
复制代码







命令行中出错截图:



请问,错误的原因?out方法,没有加同步
要注意哦!!


修改后截图:







4 个回复

倒序浏览
你out()方法根本没有同步,没有同步就没有锁,当运行到this.notify(),没有锁指定当然错了。
回复 使用道具 举报
一年之后 发表于 2013-12-17 11:09
你out()方法根本没有同步,没有同步就没有锁,当运行到this.notify(),没有锁指定当然错了。
...


谢咯!以后,我会注意的。
{:soso_e121:}

回复 使用道具 举报
public synchronized void set(String name,String sex){
                   if(flag)
                    try{this.wait();
                    this.name=name;
                           this.sex=sex;
                           flag=true;
                           this.notify();
                    }catch(Exception e){}
                    }
                  
                 }
是不是要这样子啊。。。
回复 使用道具 举报
Xiacker 发表于 2013-12-17 11:22
public synchronized void set(String name,String sex){
                   if(flag)
                    try{this.wait();

哦,是,我的那个out方法,没有加同步导致的。和try格式没关系。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马