views:

28

answers:

1

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
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
@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