LinkedBlockingQueue的insert和remove方法线程安全吗? [英] Are LinkedBlockingQueue's insert and remove methods thread safe?

查看:950
本文介绍了LinkedBlockingQueue的insert和remove方法线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的线程之间使用 LinkedBlockingQueue 。一个线程通过添加添加数据,而另一个线程通过接收接收数据。

I'm using LinkedBlockingQueue between two different threads. One thread adds data via add, while the other thread receives data via take.

我的问题是,我需要同步访问添加采取 LinkedBlockingQueue 的插入和删除方法线程安全吗?

My question is, do I need to synchronize access to add and take. Is LinkedBlockingQueue's insert and remove methods thread safe?

推荐答案

是。从文档


BlockingQueue实现是
线程安全的所有排队方法
使用
内部锁然而,批量
收集操作addAll,
containsAll,retainAll和removeAll
不一定执行
atomically除非另有规定$ b所以它是
可能,例如,对于addAll(c)
失败(抛出异常)之后
只添加
中的一些元素c。

"BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation. So it is possible, for example, for addAll(c) to fail (throwing an exception) after adding only some of the elements in c."

这篇关于LinkedBlockingQueue的insert和remove方法线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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