C
castillo.bryan
I thought I could use a ThreadPoolExecutor for a producer/consumer
relationship. I wanted to have a fixed queue size for the pool, which
blocked on the producer side if the queue was full, until a slot in the
queue was open. I can see that a RejectedExecutionHandler is called
when the queue is full and there are some pre-existing handlers, to
drop the Runnable or to run the Runnable in the current thread, but no
support for waiting until a slot is empty. I thought that running the
Runnable in the current thread is pretty close, but if multiple slots
open up, while the current thread is busy with a Runnable, it can't
give more tasks to waiting threads.
So I wrote this simple class to block until a slot is empty. Does this
seem reasonable? Does something like this already exist in the JDK that
I missed?
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class BlockingThreadPoolExecutor extends ThreadPoolExecutor {
private static class BlockingQueuePut implements
RejectedExecutionHandler {
public void rejectedExecution(Runnable r, ThreadPoolExecutor
executor) {
try {
executor.getQueue().put(r);
} catch (InterruptedException ie) {
throw new RejectedExecutionException(ie);
}
}
}
public BlockingThreadPoolExecutor(int coreThreadSize, int
maxThreadSize, int queueSize) {
super(
coreThreadSize,
maxThreadSize,
5,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(queueSize),
new BlockingQueuePut());
}
}
relationship. I wanted to have a fixed queue size for the pool, which
blocked on the producer side if the queue was full, until a slot in the
queue was open. I can see that a RejectedExecutionHandler is called
when the queue is full and there are some pre-existing handlers, to
drop the Runnable or to run the Runnable in the current thread, but no
support for waiting until a slot is empty. I thought that running the
Runnable in the current thread is pretty close, but if multiple slots
open up, while the current thread is busy with a Runnable, it can't
give more tasks to waiting threads.
So I wrote this simple class to block until a slot is empty. Does this
seem reasonable? Does something like this already exist in the JDK that
I missed?
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class BlockingThreadPoolExecutor extends ThreadPoolExecutor {
private static class BlockingQueuePut implements
RejectedExecutionHandler {
public void rejectedExecution(Runnable r, ThreadPoolExecutor
executor) {
try {
executor.getQueue().put(r);
} catch (InterruptedException ie) {
throw new RejectedExecutionException(ie);
}
}
}
public BlockingThreadPoolExecutor(int coreThreadSize, int
maxThreadSize, int queueSize) {
super(
coreThreadSize,
maxThreadSize,
5,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(queueSize),
new BlockingQueuePut());
}
}