Prevent ConcurrentLinkedQueue <T> from outofmemory Exception

When working with ConcurrentLinkedQueue, how can I limit the size of the queue. Is this possible due to exceeding the exception?

+6
java queue out-of-memory
source share
2 answers

The size of the ConcurrentLinkedQueue unlimited, so if manufacturers place items faster than consumers can remove them, in the end they will really eat. If you want to limit the size of the queue, try locking the queue, such as LinkedBlockingQueue or ArrayBlockingQueue . The size of the first may not necessarily be limited, and the second is always limited.

+5
source share

I was a bit late for this one, but you can always set the upper limit and when it is exceeded, your method will consume one token to make room for the next, etc.

0
source share

All Articles