public class BlockingThreadPoolExecutor extends ThreadPoolExecutor
ThreadPoolExecutor class.
It is based on a version originally written by Yaneeve Shekel and Amir Kirsh with the difference that a counting
Semaphore is used instead of a RejectedExecutionHandler, which makes for a simpler implementation.
Two functions had been added to this subclass.
1) The execute method of the ThreadPoolExecutor will block in case the queue is full and only unblock when
the queue is de-queued - that is a task that is currently in the queue is removed and handled by the ThreadPoolExecutor.
2) Client code can await for the event of all tasks being run to conclusion. Client code which actively chooses to
wait for this occurrence should call await() on the instance of this
ThreadPoolExecutor. This differs from ThreadPoolExecutor.awaitTermination(long, java.util.concurrent.TimeUnit) as it does not require
any call to ThreadPoolExecutor.shutdown() and may be used repeatedly.
Code example:
BlockingThreadPoolExecutor threadPoolExecutor = new BlockingThreadPoolExecutor(5);
for (int i = 0; i < 5000; i++) {
threadPoolExecutor.execute(...)
}
try {
threadPoolExecutor.await();
} catch (InterruptedException e) {
// Handle error.
}
System.out.println("Done!");
The example above shows how 5000 tasks are run within five threads. The line with System.out.println
("Done!"); will not run until such a time when all the tasks given to the thread pool have concluded their run.ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy| Constructor and Description |
|---|
BlockingThreadPoolExecutor(int poolSize) |
BlockingThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit) |
BlockingThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) |
BlockingThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler,
long maximumBlockingTime,
TimeUnit maximumBlockingUnit)
This constructor is used in order to maintain the first functionality specified above.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
afterExecute(Runnable r,
Throwable t)
After calling super's implementation of this method, the number of acquired permits is decremented.
|
void |
await()
A blocking wait for this
ThreadPoolExecutor to be in idle state, which means that there are no more tasks
in the Queue or currently executed by one of the threads. |
boolean |
await(long timeout,
TimeUnit timeUnit)
A blocking wait for this ThreadPool to be in idle state or a certain timeout to elapse.
|
void |
execute(Runnable task)
Before calling super's version of this method, a permit is acquired in order to queue the task for execution.
|
void |
setMaximumPoolSize(int maximumPoolSize)
Increase or decreases the maximum pool size by adjusting the number of permits accordingly.
|
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, terminated, toStringinvokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submitpublic BlockingThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler,
long maximumBlockingTime,
TimeUnit maximumBlockingUnit)
corePoolSize - corePoolSize the number of threads to keep in the pool, even if they are idle, unless
allowCoreThreadTimeOut is setmaximumPoolSize - the maximum number of threads to allow in the poolkeepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess
idle threads will wait for new tasks before terminating.unit - the time unit for the keepAliveTime argumenthandler - the handler to use when execution is blocked because the thread bounds and queue capacities are
reachedthreadFactory - the factory to use when the executor creates a new threadmaximumBlockingTime - is the maximum time to wait on the queue of tasks before throwing a RejectedExecutionException, or 0 to wait indefinitelymaximumBlockingUnit - is the unit of time to use with the previous parameterpublic BlockingThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
public BlockingThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit)
public BlockingThreadPoolExecutor(int poolSize)
public void execute(Runnable task)
execute in interface Executorexecute in class ThreadPoolExecutorRejectedExecutionException - if the thread is interrupted while waiting for a slot to become available
to execute a task.ThreadPoolExecutor.execute(Runnable)protected void afterExecute(Runnable r, Throwable t)
signalAll() method is invoked, thus
anyone awaiting on this instance of ThreadPoolExecutor is released.afterExecute in class ThreadPoolExecutorThreadPoolExecutor.afterExecute(Runnable, Throwable)public void setMaximumPoolSize(int maximumPoolSize)
setMaximumPoolSize in class ThreadPoolExecutorRuntimeException - if the pool size could not be decreased within the maximum blocking time.ThreadPoolExecutor.setMaximumPoolSize(int)public void await()
throws InterruptedException
ThreadPoolExecutor to be in idle state, which means that there are no more tasks
in the Queue or currently executed by one of the threads. BE AWARE that this method may get out from
blocking state when a task is currently sent to the ThreadPoolExecutor not from this thread context.
Thus it is not safe to call this method in case there are several threads feeding the ThreadPoolExecutor with tasks (calling execute). The safe way to call this method is from the thread
that is calling execute and when there is only one such thread. Note that this method differs from
awaitTermination, as it can be called without shutting down the ThreadPoolExecutor.InterruptedException - when the internal condition throws it.public boolean await(long timeout,
TimeUnit timeUnit)
throws InterruptedException
await() method, except for adding the timeout condition.false if the timeout elapsed, true if the synchronous event we are waiting for had
happenedInterruptedException - when the internal condition throws it.for more details.Copyright © 2018 The International Consortium of Investigative Journalists. All rights reserved.