title: Java BlockingQueue with batching?
body: <p>I am interested in a data structure identical to the Java BlockingQueue, with the exception that it must be able to batch objects in the queue. In other words, I would like the producer to be able to put objects into the queue, but have the consumer block on <code>take()</code> untill the queue reaches a certain size (the batch size).</p>
  
  <p>Then, once the queue has reached the batch size, the producer must block on <code>put()</code> untill the consumer has consumed all of the elements in the queue (in which case the producer will start producing again and the consumer block untill the batch is reached again).</p>
  
  <p>Does a similar data structure exist? Or should I write it (which I don't mind), I just don't want to waste my time if there is something out there.</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Maybe to clarify things a bit:</p>
  
  <p>The situation will always be as follows. There can be multiple producers adding items to the queue, but there will never be more than one consumer taking items from the queue.</p>
  
  <p>Now, the problem is that there are multiple of these setups in parallel and serial. In other words, producers produce items for multiple queues, while consumers in their own right can also be producers. This can be more easily thought of as a directed graph of producers, consumer-producers, and finally consumers.</p>
  
  <p>The reason that producers should block until the queues are empty (@Peter Lawrey) is because each of these will be running in a thread. If you leave them to simply produce as space becomes available, you will end up with a situation where you have too many threads trying to process too many things at once.</p>
  
  <p>Maybe coupling this with an execution service could solve the problem?</p>