public final class ExecutorTools
extends java.lang.Object
Executor-related utility methods.| Modifier and Type | Method and Description |
|---|---|
static void |
awaitTermination(java.util.concurrent.ExecutorService executorService)
Waits indefinitely and uninterruptibly (!)
|
static void |
awaitTermination(java.util.concurrent.ExecutorService executorService,
long interruptAfter)
Waits indefinitely and uninterruptibly (!)
|
static java.util.concurrent.ExecutorService |
createBoundedExecutor(int maxPoolSize,
java.lang.String name,
java.util.logging.Logger logger)
Creates a thread pool that creates new threads as needed, but will reuse
previously constructed threads when they are available.
|
static java.util.concurrent.ExecutorService |
createCachedExecutor(int corePoolSize,
int maxPoolSize,
boolean prestart,
Adept2ThreadFactory factory)
Creates a new CachedThreadPoolExecutor with the given initial
parameters and default rejected execution handler.
|
static java.util.concurrent.ExecutorService |
createCachedExecutor(int corePoolSize,
int maxPoolSize,
java.lang.String name,
java.util.logging.Logger logger)
Creates a new CachedThreadPoolExecutor with the given initial
parameters and default rejected execution handler.
|
static java.util.concurrent.ExecutorService |
createCurrentExecutor()
Creates a new executor service executing commands by the submitting thread.
|
static java.util.concurrent.ExecutorService |
createExecutor(int poolSize,
Adept2ThreadFactory factory,
boolean timeout)
Creates a new executor service with the designated core pool size using the
designated name and logger to create a
Adept2ThreadFactory for creating threads. |
static java.util.concurrent.ExecutorService |
createExecutor(int poolSize,
Adept2ThreadFactory factory,
boolean timeout,
int queueLimit)
Creates a new executor service with the designated core pool size using the
designated name and logger to create a
Adept2ThreadFactory for creating threads. |
static java.util.concurrent.ExecutorService |
createExecutor(int poolSize,
java.lang.String name,
java.util.logging.Logger logger,
boolean timeout)
Creates a new executor service with the designated core pool size using the
designated name and logger to create a
Adept2ThreadFactory for
creating threads. |
static java.util.concurrent.ExecutorService |
createExecutor(int poolSize,
java.lang.String name,
java.util.logging.Logger logger,
boolean timeout,
int queueLimit)
Creates a new executor service with the designated core pool size using the
designated name and logger to create a
Adept2ThreadFactory for
creating threads. |
static LoggingScheduledThreadPoolExecutor |
createScheduledExecutor(int poolSize,
Adept2ThreadFactory factory,
boolean continueRegularAfterException)
Creates a new scheduled executor service with the designated minimal pool size using the
designated factory for creating threads.
|
static LoggingScheduledThreadPoolExecutor |
createScheduledExecutor(int poolSize,
java.lang.String name,
java.util.logging.Logger logger,
boolean continueRegularAfterException)
Creates a new scheduled executor service with the designated minimal pool size using the
designated name and logger to create a
ThreadFactory for creating threads. |
public static void awaitTermination(java.util.concurrent.ExecutorService executorService)
executorService - The executor service of which to await its
termination. If this is null no waiting will take
place.public static void awaitTermination(java.util.concurrent.ExecutorService executorService,
long interruptAfter)
interruptAfter time (positive
number), ExecutorService.shutdownNow() will be called after this
time and waiting will continue.executorService - The executor service of which to await its
termination. If this is null no waiting will take
place.interruptAfter - The time in milliseconds when to interrupt the
designated executor service in case it has not terminated then.public static java.util.concurrent.ExecutorService createExecutor(int poolSize,
java.lang.String name,
java.util.logging.Logger logger,
boolean timeout)
Adept2ThreadFactory for
creating threads.poolSize - The amount of threads to keep in the pool. No additional
threads will be created.name - The name that will be used to create an
Adept2ThreadFactory for
createExecutor(int, Adept2ThreadFactory, boolean)logger - The logger that will be used to create an
Adept2ThreadFactory for
createExecutor(int, Adept2ThreadFactory, boolean).timeout - Whether unused core threads should time out. If set to
false, the core threads will be prestarted.LinkedBlockingQueue.public static java.util.concurrent.ExecutorService createExecutor(int poolSize,
java.lang.String name,
java.util.logging.Logger logger,
boolean timeout,
int queueLimit)
Adept2ThreadFactory for
creating threads.poolSize - The amount of threads to keep in the pool. No additional
threads will be created.name - The name that will be used to create an
Adept2ThreadFactory for
createExecutor(int, Adept2ThreadFactory, boolean)logger - The logger that will be used to create an
Adept2ThreadFactory for
createExecutor(int, Adept2ThreadFactory, boolean).timeout - Whether unused core threads should time out. If set to
false, the core threads will be prestarted.queueLimit - The amount of tasks in the queue before warnings will be
logged.LinkedBlockingQueue.public static java.util.concurrent.ExecutorService createExecutor(int poolSize,
Adept2ThreadFactory factory,
boolean timeout)
Adept2ThreadFactory for creating threads.poolSize - The amount of threads to keep in the pool. No additional
threads will be created.factory - The factory to use when the executor creates a new thread
and for handling (logging) exceptions occurred while executing.timeout - Whether unused core threads should time out. If set to
false, the core threads will be prestarted.LinkedBlockingQueue.public static java.util.concurrent.ExecutorService createExecutor(int poolSize,
Adept2ThreadFactory factory,
boolean timeout,
int queueLimit)
Adept2ThreadFactory for creating threads.poolSize - The amount of threads to keep in the pool. No additional
threads will be created.factory - The factory to use when the executor creates a new thread
and for handling (logging) exceptions occurred while executing.timeout - Whether unused core threads should time out. If set to
false, the core threads will be prestarted.queueLimit - The amount of tasks in the queue before warnings will be
logged.LinkedBlockingQueue.public static java.util.concurrent.ExecutorService createCurrentExecutor()
public static java.util.concurrent.ExecutorService createBoundedExecutor(int maxPoolSize,
java.lang.String name,
java.util.logging.Logger logger)
maxPoolSize - The maximum number of threads to allow in the pool.name - The name that will be used to create an
Adept2ThreadFactory for the executor.logger - The logger that will be used to create an
Adept2ThreadFactory for the executor.SynchronousQueue and the
ThreadPoolExecutor.CallerRunsPolicy as rejection execution handler.public static java.util.concurrent.ExecutorService createCachedExecutor(int corePoolSize,
int maxPoolSize,
java.lang.String name,
java.util.logging.Logger logger)
corePoolSize - The number of threads to keep in the pool, even if they
are idle.maxPoolSize - The maximum number of threads to allow in the pool.name - The name that will be used to create an
Adept2ThreadFactory for the executor.logger - The logger that will be used to create an
Adept2ThreadFactory for the executor.LinkedBlockingQueue which
will start until maxPoolSize threads, if the work load exceeds the
corePoolSize.public static java.util.concurrent.ExecutorService createCachedExecutor(int corePoolSize,
int maxPoolSize,
boolean prestart,
Adept2ThreadFactory factory)
corePoolSize - The number of threads to keep in the pool, even if they
are idle.maxPoolSize - The maximum number of threads to allow in the pool.prestart - Whether all core threads should be prestarted.factory - The factory to use when the executor creates a new thread.LinkedBlockingQueue which
will start until maxPoolSize threads, if the work load exceeds the
corePoolSize.public static LoggingScheduledThreadPoolExecutor createScheduledExecutor(int poolSize, java.lang.String name, java.util.logging.Logger logger, boolean continueRegularAfterException)
ThreadFactory for creating threads. This method
configures the service so that delayed and periodic tasks waiting for their next execution are
terminated as soon as ExecutorService#shutdown() is called. Otherwise these tasks wait for their next scheduled
execution before they terminate.ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit) and
ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit) may be
set to log runtime exceptions and virtual machine errors and continue regular execution instead
of failing and stopping.
Important: A scheduled executor service uses nanoseconds for scheduling. This may differ from milliseconds. Therefore when using milliseconds for checking conditions in a scheduled runnable, use some tolerance (at least about 50 ms) since the scheduled point in time may not have been reached in milliseconds (but in nanoseconds). See David Holmes' Weblog.
poolSize - The number of threads to keep in the pool. No additional threads will be
created.name - The name that will be used to create an Adept2ThreadFactory for
createScheduledExecutor(int, Adept2ThreadFactory, boolean).logger - The logger that will be used to create an Adept2ThreadFactory for
createScheduledExecutor(int, Adept2ThreadFactory, boolean).continueRegularAfterException - Whether regular executions should continue even in case of
runtime exceptions and virtual machine errors. These exceptions will be logged.public static LoggingScheduledThreadPoolExecutor createScheduledExecutor(int poolSize, Adept2ThreadFactory factory, boolean continueRegularAfterException)
ExecutorService#shutdown() is called.
Otherwise these tasks wait for their next scheduled execution before they terminate.ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit) and
ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit) may be
set to log runtime exceptions and virtual machine errors and continue regular execution instead
of failing and stopping.
Important: A scheduled executor service uses nanoseconds for scheduling. This may differ from milliseconds. Therefore when using milliseconds for checking conditions in a scheduled runnable, use some tolerance (at least about 50 ms) since the scheduled point in time may not have been reached in milliseconds (but in nanoseconds). See David Holmes' Weblog.
poolSize - The number of threads to keep in the pool. No additional threads will be
created.factory - The factory to use when the executor creates a new thread and for handling
(logging) exceptions occurred while executing.continueRegularAfterException - Whether regular executions should continue even in case of
runtime exceptions and virtual machine errors. These exceptions will be logged.