20

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

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.

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.

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.

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?

Shashikant Kore
  • 4,952
  • 3
  • 31
  • 40
  • here is a simple answer: most (if not all) impl. will prevent add/remove/poll, etc during drainTo, i.e. the operations will be synchronized. Here is what docs say: *Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.*, you can't rely on the behavior if the add/remove/poll/offer, etc are executed concurrently with drainTo – bestsss Jul 07 '11 at 07:52
  • 2
    @bestsss The "specified collection" refers to the collection to which the queue is being drained to. – Shashikant Kore Jul 07 '11 at 08:03
  • it's a fair point but the behavior will be just as undefined if the blocking queue allows concurrent access. – bestsss Jul 07 '11 at 09:24
  • @bestsss why do you think that? The documentation explicitly states otherwise - that BlockingQueue implementations are threadsafe. – bacar Nov 05 '14 at 10:38

3 Answers3

19

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.

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.

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.


(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.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks. I'm not concerned about atomicity. I care about thread-safety. When drainTo() is in progress, any new addition to that queue must be preserved. That's all. It doesn't matter if that element is available in the ongoing drainTo() operation or in the next one. – Shashikant Kore Jul 07 '11 at 07:47
  • 1
    @Shashikant - Well, that javadoc explicitly says that all BlockingQueue API operations are thread-safe. `drainTo` is such an operation, and therefore it is thread-safe. What more do you need to know? (Hint: if you don't believe the documentation, you can look at the source-code.) – Stephen C Jul 07 '11 at 07:52
  • 1
    Stephan, the documentation also says that bulk operation are not thread-safe. drainTo() is definitely a bulk operation. – Shashikant Kore Jul 07 '11 at 07:56
  • 1
    @Stephen, drainTo specifies that the behavior will be undefined, if the impl. allows concurrent modification. – bestsss Jul 07 '11 at 07:57
  • 1
    @bestsss where does it specify that? I see no such statement in the java 6, 7 or 8 javadoc for `BlockingQueue`. I searched for 'defined' on the page. It only mentions what happens if the collection being drained to is modified, but nothing about the implementation of the BlockingQueue interface itself allowing concurrent modifications. – bacar Nov 05 '14 at 10:36
6

drainTo() is thread safe in the sense that any operation on the queue that happens at the same time will not change the result nor will it corrupt the state of the queue. Otherwise, the method would be pretty pointless.

You could run into problems if the target collection (the one to which the results are added) does something "clever". But since you usually drain the queue to a collection to which only a single thread has access, it's more of a theoretical problem.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
2

stumbled upon this question and felt like adding an implementation info.

From Java 8 source of PriorityBlockingQueue :

 /**
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     * @throws IllegalArgumentException      {@inheritDoc}
     */
    public int drainTo(Collection<? super E> c, int maxElements) {
        if (c == null)
            throw new NullPointerException();
        if (c == this)
            throw new IllegalArgumentException();
        if (maxElements <= 0)
            return 0;
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            int n = Math.min(size, maxElements);
            for (int i = 0; i < n; i++) {
                c.add((E) queue[0]); // In this order, in case add() throws.
                dequeue();
            }
            return n;
        } finally {
            lock.unlock();
        }
    }

You can see that a ReentrantLock is used to lock the critical section. The methods poll() and offer() are also using the same lock. So the BlockingQueue implementation in this case of PriorityBlockingQueue is indeed Blocking!

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43