I am trying to execute lots of tasks using a ThreadPoolExecutor. Below is a hypothetical example:
def workQueue = new ArrayBlockingQueue<Runnable>(3, false)
def threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1L, TimeUnit.HOURS, workQueue)
for(int i = 0; i < 100000; i++)
threadPoolExecutor.execute(runnable)
The problem is that I quickly get a java.util.concurrent.RejectedExecutionException since the number of tasks exceeds the size of the work queue. However, the desired behavior I am looking for is to have the main thread block until there is room in the queue. What is the best way to accomplish this?
BlockingQueuesubclass which blocks onoffer()by delegating toput(). I think that ends up working more or less the same as theRejectedExecutionHandlerwhich callsgetQueue().put(). - Robert Tupelo-Schneckput()from within the queue itself, you don't access the queue viagetQueue()(objection #3) and the object you're putting is already properly wrapped if necessary (objection #2). You're still at risk of deadlock if all your threads die before the item comes off the queue, but that may be a risk most people looking for this particular solution would be willing to assume. - Tim