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
,java.util.concurrent.Executor
,java.util.concurrent.ExecutorService
- Direct Known Subclasses:
TimeScheduledThreadPoolExecutor
public class ThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor implements ExecutorService
AnExecutorService
that executes each submitted task using one of possibly several pooled threads, normally configured usingExecutors
factory methods.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor.AbortPolicy, java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardPolicy
-
-
Constructor Summary
Constructors Constructor Description ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue)
Creates a newThreadPoolExecutor
with the given initial parameters and default thread factory and rejected execution handler.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.RejectedExecutionHandler handler)
Creates a newThreadPoolExecutor
with the given initial parameters and default thread factory.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.ThreadFactory threadFactory)
Creates a newThreadPoolExecutor
with the given initial parameters and default rejected execution handler.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.ThreadFactory threadFactory, java.util.concurrent.RejectedExecutionHandler handler)
Creates a newThreadPoolExecutor
with the given initial parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> java.util.concurrent.RunnableFuture<T>
newTaskFor(java.lang.Runnable runnable, T value)
Returns aRunnableFuture
for the given runnable and default value.protected <T> java.util.concurrent.RunnableFuture<T>
newTaskFor(java.util.concurrent.Callable<T> callable)
Returns aRunnableFuture
for the given callable task.AsyncFuture<?>
submit(java.lang.Runnable task)
Submits a Runnable task for execution and returns a Future representing that task.<T> AsyncFuture<T>
submit(java.lang.Runnable task, T result)
Submits a Runnable task for execution and returns a Future representing that task.<T> AsyncFuture<T>
submit(java.util.concurrent.Callable<T> task)
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, toString
-
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny
-
-
-
-
Constructor Detail
-
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue)
Creates a newThreadPoolExecutor
with the given initial parameters and default thread factory and rejected execution handler. It may be more convenient to use one of theExecutors
factory methods instead of this general purpose constructor.- Parameters:
corePoolSize
- the number of threads to keep in the pool, even if they are idle, unlessallowCoreThreadTimeOut
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 thekeepAliveTime
argumentworkQueue
- the queue to use for holding tasks before they are executed. This queue will hold only theRunnable
tasks submitted by theexecute
method.- Throws:
java.lang.IllegalArgumentException
- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
java.lang.NullPointerException
- ifworkQueue
is null
-
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.ThreadFactory threadFactory)
Creates a newThreadPoolExecutor
with 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, unlessallowCoreThreadTimeOut
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 thekeepAliveTime
argumentworkQueue
- the queue to use for holding tasks before they are executed. This queue will hold only theRunnable
tasks submitted by theexecute
method.threadFactory
- the factory to use when the executor creates a new thread- Throws:
java.lang.IllegalArgumentException
- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
java.lang.NullPointerException
- ifworkQueue
orthreadFactory
is null
-
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.RejectedExecutionHandler handler)
Creates a newThreadPoolExecutor
with the given initial parameters and default thread factory.- Parameters:
corePoolSize
- the number of threads to keep in the pool, even if they are idle, unlessallowCoreThreadTimeOut
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 thekeepAliveTime
argumentworkQueue
- the queue to use for holding tasks before they are executed. This queue will hold only theRunnable
tasks submitted by theexecute
method.handler
- the handler to use when execution is blocked because the thread bounds and queue capacities are reached- Throws:
java.lang.IllegalArgumentException
- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
java.lang.NullPointerException
- ifworkQueue
orhandler
is null
-
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.ThreadFactory threadFactory, java.util.concurrent.RejectedExecutionHandler handler)
Creates a newThreadPoolExecutor
with the given initial parameters.- Parameters:
corePoolSize
- the number of threads to keep in the pool, even if they are idle, unlessallowCoreThreadTimeOut
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 thekeepAliveTime
argumentworkQueue
- the queue to use for holding tasks before they are executed. This queue will hold only theRunnable
tasks submitted by theexecute
method.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:
java.lang.IllegalArgumentException
- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
java.lang.NullPointerException
- ifworkQueue
orthreadFactory
orhandler
is null
-
-
Method Detail
-
newTaskFor
protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor(java.lang.Runnable runnable, T value)
Returns aRunnableFuture
for the given runnable and default value.- Overrides:
newTaskFor
in classjava.util.concurrent.AbstractExecutorService
- Parameters:
runnable
- the runnable task being wrappedvalue
- the default value for the returned future- Returns:
- a
RunnableFuture
which, 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
protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor(java.util.concurrent.Callable<T> callable)
Returns aRunnableFuture
for the given callable task.- Overrides:
newTaskFor
in classjava.util.concurrent.AbstractExecutorService
- Parameters:
callable
- the callable task being wrapped- Returns:
- a
RunnableFuture
which, 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
@Nonnull public AsyncFuture<?> submit(@Nonnull java.lang.Runnable task)
Description copied from interface:ExecutorService
Submits a Runnable task for execution and returns a Future representing that task. The Future'sget
method will returnnull
upon successful completion.- Specified by:
submit
in interfaceExecutorService
- Specified by:
submit
in interfacejava.util.concurrent.ExecutorService
- Overrides:
submit
in classjava.util.concurrent.AbstractExecutorService
- Parameters:
task
- the task to submit- Returns:
- a Future representing pending completion of the task
- Throws:
java.util.concurrent.RejectedExecutionException
java.lang.NullPointerException
- if the task is null
-
submit
@Nonnull public <T> AsyncFuture<T> submit(@Nonnull java.lang.Runnable task, T result)
Description copied from interface:ExecutorService
Submits a Runnable task for execution and returns a Future representing that task. The Future'sget
method will return the given result upon successful completion.- Specified by:
submit
in interfaceExecutorService
- Specified by:
submit
in interfacejava.util.concurrent.ExecutorService
- Overrides:
submit
in classjava.util.concurrent.AbstractExecutorService
- 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:
java.util.concurrent.RejectedExecutionException
java.lang.NullPointerException
- if the task is null
-
submit
@Nonnull public <T> AsyncFuture<T> submit(@Nonnull java.util.concurrent.Callable<T> task)
Description copied from interface:ExecutorService
Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future'sget
method 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
Executors
class includes a set of methods that can convert some other common closure-like objects, for example,PrivilegedAction
toCallable
form so they can be submitted.- Specified by:
submit
in interfaceExecutorService
- Specified by:
submit
in interfacejava.util.concurrent.ExecutorService
- Overrides:
submit
in classjava.util.concurrent.AbstractExecutorService
- 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:
java.util.concurrent.RejectedExecutionException
java.lang.NullPointerException
- if the task is null
-
-