class Res
{
private String name;
private int cout=1;
private boolean flag=false;
public synchronized void set(String name)
{
while (flag)
{
try
{
this.wait();
}
catch (Exception e)
{
}
}
this.name=name+"----------------"+cout++;
System.out.println(Thread.currentThread().getName()+"I am a e"+this.name);
flag=true;
this.notifyAll();
}
public synchronized void out()
{
while (!flag)
{
try
{
this.wait();
}
catch (Exception e)
{
}
}
System.out.println(Thread.currentThread().getName()+"I am an d"+"\t"+this.name);
flag=false;
this.notifyAll();
}
}
class Pro implements Runnable
{
private Res r;
Pro(Res r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.set("mao");
}
}
}
class Con implements Runnable
{
private Res r;
Con(Res r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.out();
}
}
}
class ThreadDmeo4
{
public static void main(String[] args)
{
Res r=new Res();
new Thread(new Pro(r)).start();
new Thread(new Pro(r)).start();
new Thread(new Con(r)).start();
new Thread(new Con(r)).start();
}
}
编译不过,出现了错误,而且是一堆乱码,第一次看见乱码的错误,求指点?
|
|