I feel it is better to put the throttling logic in the queue implementation itself. The reason being that the implementation is cleaner than maintaining the buffer in the executor.
The following is a way to implemented such a throttled queue:
This depends on the following callback interface:
and the following task implementation (Although you can use a different task definition as per your convenience):
In order to use the above queue, it is mandatory to use a thread pool executor where the core pool size and the max pool size is the same and all the core threads a re pre-started. The reason for this is that the executor does not enqueue it to the queue if there are spare threads that can be created. The following will be one helper class to do that:
I have just created a sample loader class to
test the above: