什么时候应该使用SynchronousQueue [英] When should I use SynchronousQueue

查看:656
本文介绍了什么时候应该使用SynchronousQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

new SynchronousQueue()
new LinkedBlockingQueue(1)


$ b b

有什么区别?当我应该使用 SynchronousQueue 对于容量为1的 LinkedBlockingQueue

推荐答案

SynchronousQueue更多的是一个切换,而LinkedBlockingQueue只允许单个元素。不同的是,对同步队列的put()调用将不会返回,直到有一个相应的take()调用,但是LinkedBlockingQueue的大小为1,put() )将立即返回。

the SynchronousQueue is more of a handoff, whereas the LinkedBlockingQueue just allows a single element. The difference being that the put() call to a SynchronousQueue will not return until there is a corresponding take() call, but with a LinkedBlockingQueue of size 1, the put() call (to an empty queue) will return immediately.

我不能说我曾经直接使用SynchronousQueue,但它是用于 Executors.newCachedThreadPool()的默认BlockingQueue, 方法。它实质上是BlockingQueue实现,当你不真的想要一个队列(你不想维护任何未决的数据)。

I can't say that i have ever used the SynchronousQueue directly myself, but it is the default BlockingQueue used for the Executors.newCachedThreadPool() methods. It's essentially the BlockingQueue implementation for when you don't really want a queue (you don't want to maintain any pending data).

这篇关于什么时候应该使用SynchronousQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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