Package com.linkedin.alpini.base.misc
Class ThreadPoolExecutor
java.lang.Object
java.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
com.linkedin.alpini.base.misc.ThreadPoolExecutor
- All Implemented Interfaces:
ExecutorService,Executor,ExecutorService
- Direct Known Subclasses:
TimeScheduledThreadPoolExecutor
An
ExecutorService that executes each submitted task using
one of possibly several pooled threads, normally configured
using Executors factory methods.-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy -
Constructor Summary
ConstructorsConstructorDescriptionThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates a newThreadPoolExecutorwith the given initial parameters and default thread factory and rejected execution handler.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) Creates a newThreadPoolExecutorwith the given initial parameters and default thread factory.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) Creates a newThreadPoolExecutorwith the given initial parameters and default rejected execution handler.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) Creates a newThreadPoolExecutorwith the given initial parameters. -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> RunnableFuture<T>newTaskFor(Runnable runnable, T value) Returns aRunnableFuturefor the given runnable and default value.protected <T> RunnableFuture<T>newTaskFor(Callable<T> callable) Returns aRunnableFuturefor the given callable task.AsyncFuture<?>Submits a Runnable task for execution and returns a Future representing that task.<T> AsyncFuture<T>Submits a Runnable task for execution and returns a Future representing that task.<T> AsyncFuture<T>Submits a value-returning task for execution and returns a Future representing the pending results of the task.Methods inherited from class java.util.concurrent.ThreadPoolExecutor
afterExecute, allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, execute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, terminated, toStringMethods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAnyMethods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow
-
Constructor Details
-
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) Creates a newThreadPoolExecutorwith the given initial parameters and default thread factory and rejected execution handler. It may be more convenient to use one of theExecutorsfactory methods instead of this general purpose constructor.- Parameters:
corePoolSize- the number of threads to keep in the pool, even if they are idle, unlessallowCoreThreadTimeOutis 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 thekeepAliveTimeargumentworkQueue- the queue to use for holding tasks before they are executed. This queue will hold only theRunnabletasks submitted by theexecutemethod.- Throws:
IllegalArgumentException- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSizeNullPointerException- ifworkQueueis null
-
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) Creates a newThreadPoolExecutorwith the given initial parameters and default rejected execution handler.- Parameters:
corePoolSize- the number of threads to keep in the pool, even if they are idle, unlessallowCoreThreadTimeOutis 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 thekeepAliveTimeargumentworkQueue- the queue to use for holding tasks before they are executed. This queue will hold only theRunnabletasks submitted by theexecutemethod.threadFactory- the factory to use when the executor creates a new thread- Throws:
IllegalArgumentException- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSizeNullPointerException- ifworkQueueorthreadFactoryis null
-
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) Creates a newThreadPoolExecutorwith the given initial parameters and default thread factory.- Parameters:
corePoolSize- the number of threads to keep in the pool, even if they are idle, unlessallowCoreThreadTimeOutis 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 thekeepAliveTimeargumentworkQueue- the queue to use for holding tasks before they are executed. This queue will hold only theRunnabletasks submitted by theexecutemethod.handler- the handler to use when execution is blocked because the thread bounds and queue capacities are reached- Throws:
IllegalArgumentException- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSizeNullPointerException- ifworkQueueorhandleris null
-
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) Creates a newThreadPoolExecutorwith the given initial parameters.- Parameters:
corePoolSize- the number of threads to keep in the pool, even if they are idle, unlessallowCoreThreadTimeOutis 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 thekeepAliveTimeargumentworkQueue- the queue to use for holding tasks before they are executed. This queue will hold only theRunnabletasks submitted by theexecutemethod.threadFactory- the factory to use when the executor creates a new threadhandler- the handler to use when execution is blocked because the thread bounds and queue capacities are reached- Throws:
IllegalArgumentException- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSizeNullPointerException- ifworkQueueorthreadFactoryorhandleris null
-
-
Method Details
-
newTaskFor
Returns aRunnableFuturefor the given runnable and default value.- Overrides:
newTaskForin classAbstractExecutorService- Parameters:
runnable- the runnable task being wrappedvalue- the default value for the returned future- Returns:
- a
RunnableFuturewhich, when run, will run the underlying runnable and which, as aFuture, will yield the given value as its result and provide for cancellation of the underlying task - Since:
- 1.6
-
newTaskFor
Returns aRunnableFuturefor the given callable task.- Overrides:
newTaskForin classAbstractExecutorService- Parameters:
callable- the callable task being wrapped- Returns:
- a
RunnableFuturewhich, when run, will call the underlying callable and which, as aFuture, will yield the callable's result as its result and provide for cancellation of the underlying task - Since:
- 1.6
-
submit
Description copied from interface:ExecutorServiceSubmits a Runnable task for execution and returns a Future representing that task. The Future'sgetmethod will returnnullupon successful completion.- Specified by:
submitin interfaceExecutorService- Specified by:
submitin interfaceExecutorService- Overrides:
submitin classAbstractExecutorService- Parameters:
task- the task to submit- Returns:
- a Future representing pending completion of the task
- Throws:
RejectedExecutionExceptionNullPointerException- if the task is null
-
submit
Description copied from interface:ExecutorServiceSubmits a Runnable task for execution and returns a Future representing that task. The Future'sgetmethod will return the given result upon successful completion.- Specified by:
submitin interfaceExecutorService- Specified by:
submitin interfaceExecutorService- Overrides:
submitin classAbstractExecutorService- Type Parameters:
T- the type of the result- Parameters:
task- the task to submitresult- the result to return- Returns:
- a Future representing pending completion of the task
- Throws:
RejectedExecutionExceptionNullPointerException- if the task is null
-
submit
Description copied from interface:ExecutorServiceSubmits a value-returning task for execution and returns a Future representing the pending results of the task. The Future'sgetmethod will return the task's result upon successful completion.If you would like to immediately block waiting for a task, you can use constructions of the form
result = exec.submit(aCallable).get();Note: The
Executorsclass includes a set of methods that can convert some other common closure-like objects, for example,PrivilegedActiontoCallableform so they can be submitted.- Specified by:
submitin interfaceExecutorService- Specified by:
submitin interfaceExecutorService- Overrides:
submitin classAbstractExecutorService- Type Parameters:
T- the type of the task's result- Parameters:
task- the task to submit- Returns:
- a Future representing pending completion of the task
- Throws:
RejectedExecutionExceptionNullPointerException- if the task is null
-