0

If I do something like:

List<Object> list= new ArrayList<Object>();
queue.drainTo(list); //BlockingQueue 

Am I assured that other threads can not put or take elements from the queue while drainTo() is being executed?

Otherwise, should I lock the whole queue before calling drainTo()?

Kami
  • 1,079
  • 2
  • 13
  • 28
  • I think you mean thread-safe instead of atomic? it is thread-safe, but not atomic: http://stackoverflow.com/questions/6607195/thread-safety-of-blockingqueues-drainto-method – fishi0x01 Jun 20 '15 at 13:58
  • No, I meant atomic. So if I do not want any concurrent put and get in the queue (which are thread-safe, but I do not want them), I have no other solution than locking the whole queue? Thanks. – Kami Jun 20 '15 at 14:08
  • You might be able to wrap the queue in an [AtomicReference](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicReference.html) - use `compareAndSet` to swap the current queue with a new queue, then other threads can continue to use the empty queue while the `drainTo` executes. – Zim-Zam O'Pootertoot Jun 20 '15 at 14:15

0 Answers0