When working with ConcurrentLinkedQueue, how can I limit the size of the Queue. Is it possible that it will through outofmemory exception ?
+2
A:
The size of a ConcurrentLinkedQueue
is unbounded, so if producers are putting in items faster than consumers can remove them, eventually it will eat up your memory indeed. If you want to limit the size of the queue, try a blocking queue, such as LinkedBlockingQueue
or ArrayBlockingQueue
instead. The size of the first can be optionally bounded, while the second is always bounded.
Péter Török
2010-10-26 08:29:38
Thanks for your answer, However I used ConcurrentLinkedQueue since it is concurrent, Are you suggesting unconcurrent classes that will require me to add synchronization when accessing these variables?
Youval
2010-10-28 08:53:58
@Youval, all `BlockingQueue` implementations are thread-safe, as declared in [the API docs](http://download.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html).
Péter Török
2010-10-28 12:59:56