BlockingQueue的rainingTo()方法的线程安全性 [英] Thread-safety of BlockingQueue's drainTo() method

查看:65
本文介绍了BlockingQueue的rainingTo()方法的线程安全性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BlockingQueue 的文档说批量操作不是线程-safe,虽然它没有明确提及方法方法toitTo().

Documentation of BlockingQueue says bulk operations are not thread-safe, though it doesn't explicitly mention the method drainTo().

BlockingQueue实现是线程安全的.所有排队方法原子地使用来达到其效果内部锁或其他形式的并发控制.但是,大部分收集操作addAll,containsAll,retainAll和removeAll不一定要执行除非另有说明,否则原子地在一个实现中.就是这样可能,例如,对于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.

drainTo()方法的文档指定了不能以线程安全的方式修改BlockingQueue元素所排放到的集合.但是,它并没有提及关于dumpTo()操作具有线程安全性的任何内容.

Documentation of drainTo() method specifies that the collection, to which the elements of BlockingQueue are drained to, cannot be modified in thread-safe fashion. But, it doesn't mention anything about drainTo() operation being thread-safe.

从中删除所有可用的元素此队列并将其添加到给定收藏.此操作可能会更多比反复轮询更有效队列.遇到失败时尝试将元素添加到集合c可能会导致元素既不存在,也不存在关联时的集合引发异常.试图流失自身的队列导致IllegalArgumentException.此外,该操作的行为是如果指定的集合,则未定义在操作进行中被修改进度.

Removes all available elements from this queue and adds them to the given collection. This operation may be more efficient than repeatedly polling this queue. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

那么,drainTo()方法是线程安全的吗?换句话说,如果一个线程在阻塞队列上调用了dumpTo()方法,而另一个线程在同一队列上调用了add()或put(),那么在两个操作结束时该队列的状态是否一致?

So, is drainTo() method thread-safe? In other words, if one thread has invoked drainTo() method on a blocking queue and other one is calling add() or put() on the same queue, is the queue's state consistent at the end of both the operations?

推荐答案

我认为您混淆了线程安全"和原子"这两个术语.他们不是同一件事.方法可以是线程安全的而不是原子的,并且可以是原子的(对于单个线程)而不是线程安全的.

I think you are confusing the terms "thread-safe" and "atomic". They do not mean the same thing. A method can be thread-safe without being atomic, and can be atomic (for a single thread) without being thread-safe.

线程安全是一个橡胶术语,很难定义为圆形.根据Goetz的说法,一个好的工作模型是,如果方法在多线程上下文中使用时是正确的",则该方法是线程安全的,因为该方法在单线程上下文中运行.除非您有正式的规范可用来衡量,否则橡胶性就是正确性是主观的.

Thread-safe is a rubbery term that is hard to define without being circular. According to Goetz, a good working model is that a method is thread-safe if it is "as correct" when used in a multi-threaded context as it is run in a single-threaded context. The rubberyness is in the fact that correctness is subjective unless you have a formal specification to measure against.

相比之下,原子很容易定义.它只是意味着该操作要么完全发生,要么根本不发生.

By contrast, atomic is easy to define. It simply means that the operation either happens completely or it doesn't happen at all.

因此,您的问题的答案是 drainTo()是线程安全的,但不是原子安全的.它不是原子的,因为它可能在耗尽过程中途抛出异常.但是,以这种方式,无论其他线程是否同时对队列进行处理,队列仍将处于一致状态.

So the answer to your question is that drainTo() is thread-safe, but not atomic. It is not atomic because it could throw an exception half way through draining. However, modulo that, the queue will still be in a consistent state, whether or not other threads were doing things to the queue at the same time.

(在上面的讨论中隐含了 BlockingQueue 接口的特定实现正确实现了该接口.如果没有实现,则所有选择都将关闭.)

(It is implicit in the above discussion that the specific implementation of the BlockingQueue interface implements the interface correctly. If it doesn't, all bets are off.)

这篇关于BlockingQueue的rainingTo()方法的线程安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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