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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© toselect 中级黑马   /  2014-6-29 23:34  /  1367 人查看  /  14 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 toselect 于 2014-7-1 19:56 编辑
  1. 代码编译通过,运行出错:提示信息 java.lang.nosuchmethod... 求解!
复制代码
  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
  10.                         {
  11.                                 this.wait();
  12.                         }
  13.                         catch (Exception e)
  14.                         {
  15.                         }
  16.                 this.name=name;
  17.                 this.sex=sex;
  18.                 flag=true;
  19.                 this.notify();
  20.         }
  21.         public synchronized void out()
  22.         {
  23.                 if(!flag)
  24.                 try
  25.                         {
  26.                                 this.wait();
  27.                         }
  28.                         catch (Exception e)
  29.                         {
  30.                         }
  31.                 System.out.println(name+"---"+sex);
  32.                 flag=false;
  33.                 this.notify();
  34.         }
  35. }
  36. class Input implements Runnable
  37. {
  38.         private Res r;
  39.         Input(Res r)
  40.         {
  41.                 this.r=r;               
  42.         }
  43.         public void run()
  44.         {
  45.                 int x=0;
  46.                 while(true)
  47.                 {
  48.                                 if(x==0)
  49.                                         r.set("老王","男");
  50.                                 else
  51.                                         r.set("霜霜","女");
  52.                                 x=(x+1)%2;
  53.                 }
  54.         }

  55. }
  56. class Output implements Runnable
  57. {
  58.         private Res r;
  59.         Output(Res r)
  60.         {
  61.                 this.r=r;               
  62.         }
  63.         public void run()
  64.         {
  65.                 while(true)
  66.                 {
  67.                  r.out();
  68.                 }
  69.         }
  70. }
  71. class  InputOutputDemo
  72. {
  73.         public static void main(String[] args)
  74.         {
  75.                 Res r=new Res();
  76.                 new Thread(new Input(r)).start();
  77.                 new Thread(new Output(r)).start();

  78.         }
  79. }
复制代码


14 个回复

倒序浏览
静丶 来自手机 中级黑马 2014-6-30 07:24:22
沙发
坐等学习~
回复 使用道具 举报
  • public synchronized void out()
  •         {
  •                 if(!flag){//这里少啦{
  •                 try
  •                         {
  •                                 this.wait();
  •                         }
  •                         catch (Exception e)
  •                         {
  •                         }
  •                 System.out.println(name+"---"+sex);
  • }//这里少啦半个}
  • //其他没错,写代码记得自己调试
  •                 flag=false;
  •                 this.notify();
  •         }

回复 使用道具 举报
学习了.
回复 使用道具 举报
多一点 发表于 2014-6-30 08:36
  • public synchronized void out()
  •         {
  •                 if(!flag){//这里少啦{

  • 不是这个问题,编译通过,运行出错。
    回复 使用道具 举报
    toselect 发表于 2014-6-30 19:45
    不是这个问题,编译通过,运行出错。

    我这运行真的没问题
    回复 使用道具 举报
    多一点 发表于 2014-6-30 22:51
    我这运行真的没问题

    把课程源代码复制粘贴,也提示同样的错误,这是版本的问题吗?
    回复 使用道具 举报
    toselect 发表于 2014-6-30 19:45
    不是这个问题,编译通过,运行出错。

    你把你的错误贴过来看看
    回复 使用道具 举报
    回帖赚币!
    回复 使用道具 举报
    多一点 发表于 2014-6-30 23:45
    你把你的错误贴过来看看


    回复 使用道具 举报
    估计是你的classPath配置上的问题
    回复 使用道具 举报
    粘贴运行表示没有任何问题啊。
    你查查你的java设置吧。

    捕获.PNG (8.62 KB, 下载次数: 2)

    捕获.PNG
    回复 使用道具 举报
    估计是你的jre中缺少相应的jar包吧!建议重新安装jre即可。
    回复 使用道具 举报
    坐等学习
    回复 使用道具 举报
    是不是你的JDK和eclipse的问题,查一下
    回复 使用道具 举报
    您需要登录后才可以回帖 登录 | 加入黑马