本帖最后由 张利 于 2012-9-14 00:39 编辑
class Res
{
private String name;
private String sex;
private boolean flag;
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","nan");
else
r.set("丽丽","女女");
x = (x+1)%2;
}
}
}
class Output implements Runnable
{
private Res r;
Output(Res r)
{
this.r=r;
}
public void run()
{
r.out();
}
}
class InputOutputDemoYH
{
public static void main(String[] args)
{
Res r = new Res();
new Thread(new Input(r)).start();
new Thread(new Output(r)).start();
}
}
跟着视频一块块的打出来的 可是运行结果是打印一句之后就锁住了 看代码也没找到是哪里的问题。。。。。。。 |
|