本帖最后由 toselect 于 2014-7-1 19:56 编辑
- 代码编译通过,运行出错:提示信息 java.lang.nosuchmethod... 求解!
复制代码- 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;
- this.notify();
- }
- public synchronized void out()
- {
- if(!flag)
- try
- {
- this.wait();
- }
- catch (Exception e)
- {
- }
- System.out.println(name+"---"+sex);
- flag=false;
- this.notify();
- }
- }
- 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("老王","男");
- else
- r.set("霜霜","女");
- x=(x+1)%2;
- }
- }
- }
- class Output implements Runnable
- {
- private Res r;
- Output(Res r)
- {
- this.r=r;
- }
- public void run()
- {
- while(true)
- {
- r.out();
- }
- }
- }
- class InputOutputDemo
- {
- public static void main(String[] args)
- {
- Res r=new Res();
- new Thread(new Input(r)).start();
- new Thread(new Output(r)).start();
- }
- }
复制代码
|
|