【发布时间】:2014-09-08 19:31:18
【问题描述】:
我有一个生产产品的生产者和一个消费产品的消费者。我想要的是,如果一个产品在 5 分钟内没有被消耗掉,我希望它被销毁。
这是制作人的部分:
boolean full = false;
public void produce(int p) throws RemoteException {
//choses a or b randomly
//if a or b spot is occupied, thread must wait()
synchronized(this){
if ((int)((Math.random()*10)%2) == 1){
while (a!=-1){try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(CHServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
a = p;
if (b!=-1) full = true;
notifyAll();
}
else {
while (b!=-1){try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(CHServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
b = p;
if (a!=-1) full = true;
notifyAll();
}
}
}
a & b 应该是我的产品。
我真的不知道如何测量该时间,例如当线程等待或客户端未尝试使用该产品时。这段代码在 RMI java 服务器上运行。
【问题讨论】:
标签: java multithreading concurrency thread-safety rmi