I have a Producer/Consumer usecase which is a bit unusual. I have a real world use case with some producers which I want them to be able to add objects into a collection without blocking. The consumer (just one) should block until a certain amount of objects are available in the collection (eg. 500) and then consume them in bulk. While there are less than 500 it should block and wait for the collection to fill. I don't mind if the queue exceeds this value (700, 1000 etc.) for short amount of times.
I currently don't seem to find a solution to fix this exact problem. I was thinking about using a ConcurrentLinkedQueue and to have the consumer periodically check if the queue has enough data, but that seems rather counterproductive.
Another idea is to use a LinkedBlockingQueue. The producers are not going to block (unless the queue is full which means it has Integer.MAX_VALUE values - this is not my case, so it's all good). The consumer will do a queue.take() and add the elements into an internal collection. When the internal collection reaches 500 elements it is going to consume them in batch.
Do you have any tips?
Thanks!