0

If I have bq1.drainTo(bq2) where bq1 and bq2 are BlockingQueues that are both accessible from multiple threads, do I need to synchronize the draining with any other reads/writes I have to bq2? If yes, it seems that's sort of eliminating the advantage of a BlockingQueue in the first place - is there a way around it? If no, why not? If sometimes, in what cases?

Kvass
  • 8,294
  • 12
  • 65
  • 108

1 Answers1

0

You can use another blockingqueue todrain, but if the actual implementation is bounded (limited capacity), then if bg2 gets full during the drainage the exception will be thrown and the state of the drainTo operation will be unknown.

Zielu
  • 8,312
  • 4
  • 28
  • 41
  • My issue isn't about an exception being thrown - it's about what whether `bq2` can be modified / accessed during this drainage and whether the drain statement needs to be synchronized to prevent that. – Kvass Mar 20 '15 at 01:35
  • it can if you dont care about the order of the elements. The BQ is thread safe so writing/reading to it will not cause problem, but there is no guarantee that all the 'drained elements' are added atomically so others, new objects may be inserted between them. You should worry about the exception though, as it won't be catch and the content being drained may be lost (unless in unbounded blockingqueue like LinkedBlockingQueue). – Zielu Mar 20 '15 at 01:40