Class NamingThreadPoolExecutor
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- java.util.concurrent.ThreadPoolExecutor
-
- ch.systemsx.cisd.base.namedthread.NamingThreadPoolExecutor
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,java.util.concurrent.ExecutorService
public class NamingThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor
AThreadPoolExecutor
that allows to attach names to the threads it manages. These names can come either fromIRunnableNameProvider
s orICallableNameProvider
s, or, if their standard counterparts are submitted, a default name is used.
-
-
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
-
-
Field Summary
Fields Modifier and Type Field Description static long
DEFAULT_KEEP_ALIVE_TIME_MILLIS
The default time (in milli-seconds) to keep threads alive that are above the core pool size.
-
Constructor Summary
Constructors Constructor Description NamingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, NamingThreadFactory threadFactory)
Creates a new NamingThreadPoolExecutor with the given initial parameters.NamingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, NamingThreadFactory threadFactory, java.util.concurrent.RejectedExecutionHandler handler)
Creates a new NamingThreadPoolExecutor with the given initial parameters.NamingThreadPoolExecutor(java.lang.String poolName)
Creates a new (caching) NamingThreadPoolExecutor with the given initial parameters.NamingThreadPoolExecutor(java.lang.String poolName, int workQueueSize)
Creates a new (caching) NamingThreadPoolExecutor with the given initial parameters.NamingThreadPoolExecutor(java.lang.String poolName, int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue)
Creates a new NamingThreadPoolExecutor with the given initial parameters.NamingThreadPoolExecutor(java.lang.String poolName, 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 new NamingThreadPoolExecutor with the given initial parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description NamingThreadPoolExecutor
addPoolName(boolean addPoolName)
If addPoolName istrue
, the threads will contain the pool name as the first part of the thread names.protected void
afterExecute(java.lang.Runnable r, java.lang.Throwable t)
protected void
beforeExecute(java.lang.Thread t, java.lang.Runnable r)
NamingThreadPoolExecutor
corePoolSize(int corePoolSize)
Same asThreadPoolExecutor.setCorePoolSize(int)
, but returns the object itself for chaining.NamingThreadPoolExecutor
daemonize()
Sets the thread factory of this pool executor to daemon creation mode.NamingThreadFactory
getThreadFactory()
NamingThreadPoolExecutor
keepAliveTime(long keepAliveTimeMillis)
Same asThreadPoolExecutor.setKeepAliveTime(long, TimeUnit)
, but uses alwaysTimeUnit.MILLISECONDS
and returns the object itself for chaining.NamingThreadPoolExecutor
maximumPoolSize(int maximumPoolSize)
Same asThreadPoolExecutor.setMaximumPoolSize(int)
, but returns the object itself for chaining.void
setThreadFactory(NamingThreadFactory threadFactory)
Sets the thread factory of this pool executor.void
setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Deprecated.UsesetThreadFactory(NamingThreadFactory)
instead!java.util.concurrent.Future<?>
submit(java.lang.Runnable task)
<T> java.util.concurrent.Future<T>
submit(java.lang.Runnable task, T result)
<T> java.util.concurrent.Future<T>
submit(java.util.concurrent.Callable<T> task)
-
Methods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, execute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, shutdown, shutdownNow, terminated, toString
-
-
-
-
Field Detail
-
DEFAULT_KEEP_ALIVE_TIME_MILLIS
public static final long DEFAULT_KEEP_ALIVE_TIME_MILLIS
The default time (in milli-seconds) to keep threads alive that are above the core pool size.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
NamingThreadPoolExecutor
public NamingThreadPoolExecutor(java.lang.String poolName)
Creates a new (caching) NamingThreadPoolExecutor with the given initial parameters. This executor will create new threads as needed.- Parameters:
poolName
- the default name for new threads
-
NamingThreadPoolExecutor
public NamingThreadPoolExecutor(java.lang.String poolName, int workQueueSize)
Creates a new (caching) NamingThreadPoolExecutor with the given initial parameters. This executor will create new threads as needed.- Parameters:
poolName
- The default name for new threads.workQueueSize
- The size of the work queue (0 for no queue).
-
NamingThreadPoolExecutor
public NamingThreadPoolExecutor(java.lang.String poolName, 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 new NamingThreadPoolExecutor with the given initial parameters.- Parameters:
poolName
- the default name for new threadscorePoolSize
- the number of threads to keep in the pool, even if they are idle.maximumPoolSize
- the maximum number of threads to allow in the pool.keepAliveTime
- 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 argument.workQueue
- the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.handler
- the handler to use when execution is blocked because the thread bounds and queue capacities are reached.- Throws:
java.lang.IllegalArgumentException
- if corePoolSize, or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.java.lang.NullPointerException
- if workQueue or threadFactory or handler are null.
-
NamingThreadPoolExecutor
public NamingThreadPoolExecutor(java.lang.String poolName, int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue)
Creates a new NamingThreadPoolExecutor with the given initial parameters.- Parameters:
poolName
- the default name for new threadscorePoolSize
- the number of threads to keep in the pool, even if they are idle.maximumPoolSize
- the maximum number of threads to allow in the pool.keepAliveTime
- 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 argument.workQueue
- the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.- Throws:
java.lang.IllegalArgumentException
- if corePoolSize, or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.java.lang.NullPointerException
- if workQueue or threadFactory are null.
-
NamingThreadPoolExecutor
public NamingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, NamingThreadFactory threadFactory, java.util.concurrent.RejectedExecutionHandler handler)
Creates a new NamingThreadPoolExecutor with the given initial parameters.- Parameters:
corePoolSize
- the number of threads to keep in the pool, even if they are idle.maximumPoolSize
- the maximum number of threads to allow in the pool.keepAliveTime
- 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 argument.workQueue
- the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.threadFactory
- the factory to use when the executor creates a new thread.handler
- the handler to use when execution is blocked because the thread bounds and queue capacities are reached.- Throws:
java.lang.IllegalArgumentException
- if corePoolSize, or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.java.lang.NullPointerException
- if workQueue or threadFactory or handler are null.
-
NamingThreadPoolExecutor
public NamingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, NamingThreadFactory threadFactory)
Creates a new NamingThreadPoolExecutor with the given initial parameters.- Parameters:
corePoolSize
- the number of threads to keep in the pool, even if they are idle.maximumPoolSize
- the maximum number of threads to allow in the pool.keepAliveTime
- 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 argument.workQueue
- the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.threadFactory
- the factory to use when the executor creates a new thread.- Throws:
java.lang.IllegalArgumentException
- if corePoolSize, or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.java.lang.NullPointerException
- if workQueue or threadFactory are null.
-
-
Method Detail
-
daemonize
public NamingThreadPoolExecutor daemonize()
Sets the thread factory of this pool executor to daemon creation mode.This method is supposed to be used in chaining mode, i.e.
final ExecutorService executor = new NamingThreadPoolExecutor("name").daemonize();
- Returns:
- This class itself.
-
corePoolSize
public NamingThreadPoolExecutor corePoolSize(int corePoolSize)
Same asThreadPoolExecutor.setCorePoolSize(int)
, but returns the object itself for chaining.
-
maximumPoolSize
public NamingThreadPoolExecutor maximumPoolSize(int maximumPoolSize)
Same asThreadPoolExecutor.setMaximumPoolSize(int)
, but returns the object itself for chaining.
-
keepAliveTime
public NamingThreadPoolExecutor keepAliveTime(long keepAliveTimeMillis)
Same asThreadPoolExecutor.setKeepAliveTime(long, TimeUnit)
, but uses alwaysTimeUnit.MILLISECONDS
and returns the object itself for chaining.
-
addPoolName
public NamingThreadPoolExecutor addPoolName(boolean addPoolName)
If addPoolName istrue
, the threads will contain the pool name as the first part of the thread names.
-
getThreadFactory
public NamingThreadFactory getThreadFactory()
- Overrides:
getThreadFactory
in classjava.util.concurrent.ThreadPoolExecutor
-
setThreadFactory
public void setThreadFactory(NamingThreadFactory threadFactory)
Sets the thread factory of this pool executor.
-
setThreadFactory
@Deprecated public void setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Deprecated.UsesetThreadFactory(NamingThreadFactory)
instead!- Overrides:
setThreadFactory
in classjava.util.concurrent.ThreadPoolExecutor
-
beforeExecute
protected void beforeExecute(java.lang.Thread t, java.lang.Runnable r)
- Overrides:
beforeExecute
in classjava.util.concurrent.ThreadPoolExecutor
-
afterExecute
protected void afterExecute(java.lang.Runnable r, java.lang.Throwable t)
- Overrides:
afterExecute
in classjava.util.concurrent.ThreadPoolExecutor
-
submit
public java.util.concurrent.Future<?> submit(java.lang.Runnable task)
- Specified by:
submit
in interfacejava.util.concurrent.ExecutorService
- Overrides:
submit
in classjava.util.concurrent.AbstractExecutorService
-
submit
public <T> java.util.concurrent.Future<T> submit(java.lang.Runnable task, T result)
- Specified by:
submit
in interfacejava.util.concurrent.ExecutorService
- Overrides:
submit
in classjava.util.concurrent.AbstractExecutorService
-
submit
public <T> java.util.concurrent.Future<T> submit(java.util.concurrent.Callable<T> task)
- Specified by:
submit
in interfacejava.util.concurrent.ExecutorService
- Overrides:
submit
in classjava.util.concurrent.AbstractExecutorService
-
-