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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


  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.         {
  8.                 if(flag)
  9.                         try{this.wait();}catch(Exception e){}

  10.                 this.name=name;

  11.                 this.sex=sex;

  12.                 flag=true;
  13.                 this.notify();
  14.         }
  15.         public void out()
  16.         {
  17.                 if(!flag)
  18.                         try{this.wait();}catch(Exception e){}
  19.                 System.out.println(name+"........."+sex);
  20.                 flag=false;
  21.                 this.notify();
  22.         }
  23. }
  24. class Input implements Runnable
  25. {
  26.         private Res 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.                         else
  39.                                 r.set("丽丽","women");
  40.                         x=(x+1)%2;       
  41.                 }
  42.         }
  43. }
  44. class Output implements Runnable
  45. {
  46.         private Res r;
  47.         Output(Res r)
  48.         {
  49.                 this.r=r;
  50.         }
  51.         public void run()
  52.         {
  53.                 while(true)
  54.                 {
  55.                         r.out();
  56.                
  57.                 }
  58.         }
  59. }
  60. class  InputOutputDemo
  61. {
  62.         public static void main(String[] args)
  63.         {
  64.                 Res r=new Res();
  65.                
  66.                 new Thread(new Input(r)).start();
  67.                 new Thread(new Output(r)).start();


  68.                
  69.         }
  70. }
复制代码


代码优化.png (8 KB, 下载次数: 3)

错误提示

错误提示

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

7 个回复

倒序浏览
违法的监控状态异常。当某个线程试图等待一个自己并不拥有的对象(O)的监控器或者通知其他线程等待该对象(O)的监控器时,抛出该异常。如果当前线程不是对象所得持有者,该方法抛出一个java.lang.IllegalMonitorStateException 异常”

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
全部代码在上面,能帮我看一下哪里出错了么?谢谢
回复 使用道具 举报
对于notify这样处理就好啦,两个都要处理
  1. try{this.notify();}
  2.                 catch(Exception e){System.exit(0);}
复制代码
回复 使用道具 举报
完整代码:
  1. package com.itheima;

  2. public class Diary3 {
  3.           public static void main(String[] args)
  4.       {
  5.               Res r=new Res();
  6.               
  7.               new Thread(new Input(r)).start();
  8.               new Thread(new Output(r)).start();


  9.               
  10.       }

  11. }

  12. class Res
  13. {
  14.         private String name;
  15.         private String sex;
  16.         private boolean flag=false;

  17.         public synchronized void set(String name,String sex)
  18.         {
  19.                 if(flag)
  20.                        try{this.wait();}
  21.                 catch(Exception e){}

  22.                 this.name=name;

  23.                 this.sex=sex;

  24.                 flag=true;
  25.                 try{this.notify();}
  26.                 catch(Exception e){System.exit(0);}
  27.         }
  28.         public void out()
  29.         {
  30.                 if(!flag)
  31.                        try{this.wait();}
  32.                 catch(Exception e){}
  33.                 System.out.println(name+"........."+sex);
  34.                 flag=false;
  35.                 try{this.notify();}
  36.                 catch(Exception e){System.exit(0);}
  37.                
  38.         }
  39. }
  40. class Input implements Runnable
  41. {
  42.         private Res r;
  43.         Input(Res r)
  44.         {
  45.                 this.r=r;
  46.         }
  47.         public void run()
  48.         {
  49.                 int x=0;
  50.                 while(true)
  51.                 {
  52.                         if (x==0)
  53.                                 r.set("mike","man");
  54.                         else
  55.                                 r.set("丽丽","women");
  56.                         x=(x+1)%2;        
  57.                 }
  58.         }
  59. }
  60. class Output implements Runnable
  61. {
  62.         private Res r;
  63.         Output(Res r)
  64.         {
  65.                 this.r=r;
  66.         }
  67.         public void run()
  68.         {
  69.                 while(true)
  70.                 {
  71.                         r.out();
  72.                
  73.                 }
  74.         }
  75. }
复制代码
回复 使用道具 举报 1 0
package com.itheima;

public class Diary3 {
          public static void main(String[] args)
      {
              Res r=new Res();
              
              new Thread(new Input(r)).start();
              new Thread(new Output(r)).start();


              
      }

}

class Res
{
        private String name;
        private String sex;
        private boolean flag=false;

        public synchronized void set(String name,String sex)
        {
                if(flag)
                       try{this.wait();}
                catch(Exception e){}

                this.name=name;

                this.sex=sex;

                flag=true;
                try{this.notify();}
                catch(Exception e){System.exit(0);}
        }
        public synchronized void out()
        {
                if(!flag)
                       try{this.wait();}
                catch(Exception e){}
                System.out.println(name+"........."+sex);
                flag=false;
                try{this.notify();}
                catch(Exception e){System.exit(0);}
               
        }
}
class Input implements Runnable
{
        private Res r;
        Input(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                int x=0;
                while(true)
                {
                        if (x==0)
                                r.set("mike","man");
                        else
                                r.set("丽丽","women");
                        x=(x+1)%2;        
                }
        }
}
class Output implements Runnable
{
        private Res r;
        Output(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                while(true)
                {
                        r.out();
               
                }
        }
}
亲测好用

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
你的out方法也应该是同步的。你再看看视频是不是你看错了呀
回复 使用道具 举报
看到这段代码,果断給跪了!学习第四天伤不起啊!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马