concurrentLinkedQueue 提供/轮询阻塞 [英] concurrentLinkedQueue offer/poll blocking

查看:104
本文介绍了concurrentLinkedQueue 提供/轮询阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否提供区块轮询,反之亦然?意思是,生产者可以提供,同时消费者试图进行投票吗?或者如果生产者提供,队列会阻塞直到他完成?

does offer blocks poll or vice versa ? meaning , can a producer offer and at the same time a consumer trying to poll ? or if a producer was offering , the queue blocks till he is done ?

对象 A

  while (true){
    inputQueue.offer(newPartList);
}

对象 B

     while (true){
    inputQueue.poll(newPartList);
}

推荐答案

不,它没有.改用 LinkedBlockingDeque.请记住使用来自 BlockingDequeue 的方法或BlockingQueue 获取值的接口.因为这些方法被实现为阻塞.

No, it does not. use LinkedBlockingDeque instead. Remember to use methods from BlockingDequeue or BlockingQueue interfaces to get values. as these methods are implemented as blocking.

更新

offerpoll 方法都不会阻塞.在链接接口中,只有 put*take* 方法被实现为阻塞.put 只要队列已满就会阻塞,如果队列为空,take 就会阻塞.

Neither offer nor poll methods are blocking. In linked interfaces only put* and take* methods are implemented as blocking. put blocks as long as the queue is full, take blocks if the queue is empty.

现在我看到你在问其他问题.在 LinkedBlockingDeque 中调用 offer 阻塞了 poll,因为这个实现有内部的 Lock 用于同步访问.在 ConcurrentLinkedQueue's JavaDoc 中明确说明,即:

Now I see You are asking something else. In LinkedBlockingDeque calling offer blocks poll, because this implementation has inernal Lock for synchronizing access. In ConcurrentLinkedQueue's JavaDoc it is explicitly stated, that:

此实现采用了一种高效的无等待"算法,其基于简单、快速且实用的非阻塞和阻塞并发队列算法by Maged M. Michael 和 Michael L. Scott.

这表明这些操作不会相互阻塞.如果您有兴趣,我强烈建议您阅读该论文以获得更多见解.

which suggests that these operations don't block each other. If You are interested, I strongly suggest reading that paper for more insight.

这篇关于concurrentLinkedQueue 提供/轮询阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆