public class MostExecutors extends Object
Modifier and Type | Class and Description |
---|---|
static class |
MostExecutors.NamedThreadFactory
A ThreadFactory which gives each thread a meaningful and distinct name.
|
Modifier and Type | Method and Description |
---|---|
static ForkJoinPool |
forkJoinPoolWithThreadLimit(int parallelism,
int spares)
Construct a ForkJoinPool with a stricter thread limit.
|
static ExecutorService |
newMultiThreadExecutor(String threadName,
int count)
Creates a multi-threaded executor with meaningfully named threads.
|
static ExecutorService |
newMultiThreadExecutor(ThreadFactory threadFactory,
int count) |
static ExecutorService |
newSingleThreadExecutor(String threadName)
Creates a single threaded executor that silently discards rejected tasks.
|
static ExecutorService |
newSingleThreadExecutor(ThreadFactory threadFactory) |
static boolean |
shutdown(ExecutorService service,
long timeout,
TimeUnit unit)
Shutdown
service and wait for all it's tasks to terminate. |
static void |
shutdownOrThrow(ExecutorService service,
long timeout,
TimeUnit unit,
RuntimeException exception)
Cancel the processing being carried out by the given service and waits for the processing to
complete.
|
public static ExecutorService newSingleThreadExecutor(String threadName)
Executors.newSingleThreadExecutor()
is that it does not let us
specify a RejectedExecutionHandler, which we need to ensure that garbage is not spewed to the
user's console if the build fails.threadName
- a thread name prefix used to easily identify threads when debugging.public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)
public static ExecutorService newMultiThreadExecutor(String threadName, int count)
threadName
- a thread name prefix used to easily identify threads when debugging.count
- the number of threads that should be created in the pool.public static ExecutorService newMultiThreadExecutor(ThreadFactory threadFactory, int count)
public static boolean shutdown(ExecutorService service, long timeout, TimeUnit unit) throws InterruptedException
service
and wait for all it's tasks to terminate. In the event of InterruptedException
, propagate the interrupt to all tasks, wait for them to finish, then
re-throw the exception.InterruptedException
public static void shutdownOrThrow(ExecutorService service, long timeout, TimeUnit unit, RuntimeException exception)
public static ForkJoinPool forkJoinPoolWithThreadLimit(int parallelism, int spares)
ForkJoinPool by default will create a new thread to handle pending work whenever an existing thread becomes blocked on a task and cannot work steal. In cases when many tasks would block on a slow running dependency, it can trigger thread creation for all those tasks.
Note that limiting the maximum threads will impact the ability for ManagedBlockers to cause the pool to create new worker threads, leading to potential deadlock if many ManagedBlockers are used.