Redian新闻
>
有没有Blocking Bounded Queue 用Pthread实现的Sample Code?
avatar
有没有Blocking Bounded Queue 用Pthread实现的Sample Code?# JobHunting - 待字闺中
k*t
1
要用两个conditional variable吗?谢了。
avatar
c*r
2
Pthread 没有,有一个thread的
public class BlockingQueue {
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public synchronized void enqueue(Object item) throws InterruptedException{
while(this.queue.size() == this.limit){
wait();
}
if(this.queue.isEmpty()){
notifyAll();
}

this.queue.add(item);
}
public synchronized Object dequeue() throws InterruptedException {
while(this.queue.isEmpty()){
wailt();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
}
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。