Offer 是否会阻止 poll,反之亦然?意思是,生产者可以提供报价,同时消费者可以尝试进行民意调查吗?或者如果生产者正在提供,队列会阻塞直到他完成为止?
object A
while (true){
inputQueue.offer(newPartList);
}
object B
while (true){
inputQueue.poll(newPartList);
}
不,不是的。使用链接阻塞双端队列 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html反而。请记住使用以下方法阻塞出队 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html or 阻塞队列 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html接口来获取值。因为这些方法是作为阻塞实现的。
Update
Neither offer
nor poll
方法被阻塞。仅在链接接口中put*
and take*
方法被实现为阻塞。put
只要队列已满就会阻塞,take
如果队列为空则阻塞。
现在我明白你在问别的事情。
在链接阻塞双端队列 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html呼叫offer
blocks poll
,因为这个实现有内部Lock
用于同步访问。在并发链接队列 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.htmlJavaDoc 明确指出:
This implementation employs an efficient "wait-free" algorithm based on one described in
简单、快速、实用的非阻塞和阻塞并发队列算法 http://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdfby Maged M. Michael and Michael L. Scott.
这表明这些操作不会互相阻塞。如果您有兴趣,我强烈建议您阅读该论文以获得更多见解。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)