classRes{privateStringname;privateStringsex;privatebooleanflag=false;publicsynchronizedvoidset(Stringname,Stringsex){if(flag)try{this.wait();}catch(Exceptione){}this.name...
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("mike","man");
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 haha
{
public static void main(String[] args)
{
Res r=new Res();
Input in=newInput(r);
Output out=newOutput(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
他在我代码的最后面提示错误说到达文件结尾,代码最后是到达结尾没错,干嘛提示错误
展开