本帖最后由 贠(yun)靖 于 2012-3-15 19:56 编辑
class Demo
{
public static void main(String[] args)
{
Resource r=new Resource();
Input in = new Input(r);
Output out = new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
class Resource
{
String name;
String sex;
boolean flag=false;
public void set(String name,String sex)
{
int x=0;
while (true)
{
synchronized(this)
{
while (this.flag)
{
try
{
this.wait();
}
catch (Exception e)
{
}
}
this.name=name;
this.sex=sex;
this.flag=true;
this.notify();
}
}
}
public void out()
{
while(!this.flag)
{
try
{
this.wait();
}
catch (Exception e)
{
}
}
System.out.println(this.name+"......"+this.sex); Demo.java:77: 无法将 Resource 中的 out() 应用于 (java.lang.String,java.lang.String)
this.flag=false;
this.notify(); r.out("mmmm","nnnnnn"); - - 问题找到了 呵呵
}
}
class Input implements Runnable
{
Resource r=null;
Input(Resource r)
{
this.r=r;
}
public void run()
{
int x=0;
while(true)
{
if (x==0)
{
r.set("mmmm","nnnnnn");
}
else
r.set("小红","女女女女......");
x=(x+1)%2;
r.flag=true;
r.notify();
}
}
}
class Output implements Runnable
{
Resource r;
Output(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
synchronized(r)
{
r.out();
}
}
} |
|