public class Test3 {
public static void main(String[] args){
Reso r=new Reso();//在new Reso()下面给我画个红曲线 Eclipse运行不了
Pru p=new Pru(r);
Con c=new Con(r);
Thread t=new Thread(p);
Thread t1=new Thread(c);
Thread t2=new Thread(p);
Thread t3=new Thread(c);
t.start();
t1.start();
t2.start();
t3.start();
}
}
class Reso
{private String name;
private int count=1;
private boolean flag=false;
Reso(String name){
this.name=name+"..."+count++;
}
public synchronized void set(String name){
while(flag)
try{wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"生产者"+this.name);
flag=true;
this.notifyAll();
}
public synchronized void out(){
while(!flag)
try{wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"消费者....."+this.name);
flag=false;
this.notifyAll();
}
}
class Pru implements Runnable
{
private Reso r;
Pru(Reso r){
this.r=r;
}
public void run(){
while(true){
r.set("商品");
}
}
}
class Con implements Runnable
{
private Reso r;
Con(Reso r){
this.r=r;
}
public void run(){
while(true){
r.out();
}
}
}
————————————Reso r=new Reso();//在new Reso()下面给我画个红曲线 Eclipse运行不了
求大神解答
|
|