import java.util.concurrent.locks.*;
public class Test {
public static void main(String[] args){
res a=new res();
Pru p=new Pru(a);
Con c=new Con(a);
Thread t1=new Thread(p);
Thread t2=new Thread(c);
Thread t3=new Thread(p);
Thread t4=new Thread(c);
}
}
class res{
private String name;
private int count=1;
private boolean flag=false;
private Lock lock=new ReentrantLock();
private Condition condition_p=lock.newCondition();
private Condition condition_c=lock.newCondition();
res(String name){
this.name=name+"..."+count++;
}
public void set(String name)throws InterruptedException{
lock.lock();
try{while(flag)
condition_p.await();
System.out.println(Thread.currentThread().getName()+"生产者:::"+this.name);
condition_c.signalAll();
}
finally{
lock.unlock();
}
}
public void out()throws InterruptedException{
lock.lock();
try{while(!flag)
condition_c.signal();
System.out.println(Thread.currentThread().getName()+"消费者:::"+this.name);
condition_p.signalAll();
}
finally{
lock.unlock();
}
}
}
class Pru implements Runnable{
private res r;
Pru(res r){
this.r=r;
}
public void run(){
try{while(true)
r.set("商品");
}
catch(Exception e){
}
}
}
class Con implements Runnable{
private res r;
Con(res r){
this.r=r;
}
public void run(){
try{while(true)
r.out();
}
catch(Exception e){
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor reas() is undefined
at itheima.Test.main(Test.java:7)
}//为啥在Eclipse上会出现这个问题 求大神解释
|
|