Can ConcurrentLinkedDeque have a fixed size and overwrite old elements?

Multi tool use
Can ConcurrentLinkedDeque have a fixed size and overwrite old elements?
If I got this right ConcurrentLinkedDeque acts as a stack right if you use pollLast()
?
pollLast()
Now my issue is that I need a set size of ConcurrentLinkedDeque. My producer doesn't stop, so even if I have 16GB of ram I will run out eventually. SO is it possible to se a fixed size ?
My implementation:
ConcurrentLinkedDeque<String> queue = new ConcurrentLinkedDeque<>();
Producer (Thread 1): runs queue.add(line);
Consumer (Thread 2): runs queue.pollLast();
queue.add(line);
queue.pollLast();
Please note that both threads run in a while true loop. This is because of the requirements. That is why I am using ConcurrentLinkedDeque
and not ArrayBlockingQueue
or SynchronousQueue
because it is non-blocking.
ConcurrentLinkedDeque
ArrayBlockingQueue
SynchronousQueue
Also do I need to declare anything synchronised
?
synchronised
BlockingQueue
1 Answer
1
Can ConcurrentLinkedDeque
have a fixed size?
ConcurrentLinkedDeque
No, it's "an unbounded concurrent Deque
based on linked nodes."
Deque
Do I need to declare anything synchronised
?
synchronised
ConcurrentLinkedDeque
itself is thread-safe. Synchronisation is required only for composite actions (like overwriting old elements).
ConcurrentLinkedDeque
Can ConcurrentLinkedDeque
overwrite old elements?
ConcurrentLinkedDeque
I don't think there is such a method. That's a composite action that requires
These three actions should be performed within a synchronized
block.
synchronized
I guess i have to buy more RAM....
– Trt Trt
Jul 1 at 13:13
@TrtTrt, does the producer know the size you want to limit the collection to?
– Andrew Tobilko
Jul 1 at 13:16
@TrtTrt, you could estimate the size of the collection before putting a new element in
– Andrew Tobilko
Jul 1 at 13:17
@TrtTrt, if there's no room for a new item, you can wait (make the producer wait) until the consumer takes an element (frees some space) and notifies the producer
– Andrew Tobilko
Jul 1 at 13:20
@TrtTrt, that's the classical producer-consumer problem - I should have mentioned it earlier
– Andrew Tobilko
Jul 1 at 13:22
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use a fixed size
BlockingQueue
which would allow you to apply back pressure.– Boris the Spider
Jul 1 at 13:31