public class PredictableExecutor extends AbstractExecutorService implements ExecutorService
ExecutorService which behaves similar to the one created by
Executors.newFixedThreadPool(int, ThreadFactory),
but assigns command to specific thread based on
it's PredictableExecutor.PickyTask.hint(), Object.hashCode(), or hint value explicitly
specified when the command was passed to this PredictableExecutor.
| Modifier and Type | Class and Description |
|---|---|
static interface |
PredictableExecutor.PickyCallable<T>
Callable also implementing PredictableExecutor.PickyTask. |
static class |
PredictableExecutor.PickyFutureTask<T>
A
FutureTask implementing PredictableExecutor.PickyTask. |
static interface |
PredictableExecutor.PickyRunnable
Runnable also implementing PredictableExecutor.PickyTask. |
static interface |
PredictableExecutor.PickyTask
Abstraction to give a task a way to express it's preference to run on
certain thread.
|
| Constructor and Description |
|---|
PredictableExecutor(int buckets,
ThreadFactory threadFactory)
Creates
PredictableExecutor instance. |
PredictableExecutor(ThreadFactory threadFactory)
Creates
PredictableExecutor instance with
bucket size set to number of available processors. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit) |
protected ExecutorService |
backendExecutorService(ThreadFactory threadFactory)
Creates a single thread
ExecutorService to use in the backend. |
void |
execute(Runnable command) |
void |
execute(Runnable command,
Function<Runnable,Integer> hintFunction)
Executes given command at some time in the future.
|
void |
execute(Runnable command,
int hint)
Executes given command at some time in the future.
|
boolean |
isShutdown() |
boolean |
isTerminated() |
static PredictableExecutor |
newPredictableExecutor(int buckets,
ThreadFactory threadFactory)
Creates
PredictableExecutor instance. |
protected <T> PredictableExecutor.PickyFutureTask<T> |
newTaskFor(Callable<T> callable) |
protected <T> PredictableExecutor.PickyFutureTask<T> |
newTaskFor(Runnable runnable,
T value) |
static <T> PredictableExecutor.PickyCallable<T> |
picky(Callable<T> callable,
Function<Callable<T>,Integer> hint)
Wraps the given
Callable into PredictableExecutor.PickyCallable returning supplied hint. |
static <T> PredictableExecutor.PickyCallable<T> |
picky(Callable<T> callable,
int hint)
Wraps the given
Callable into PredictableExecutor.PickyCallable returning supplied hint. |
static PredictableExecutor.PickyRunnable |
picky(Runnable runnable,
Function<Runnable,Integer> hint)
Wraps the given
Runnable into PredictableExecutor.PickyRunnable returning supplied hint. |
static PredictableExecutor.PickyRunnable |
picky(Runnable runnable,
int hint)
Wraps the given
Runnable into PredictableExecutor.PickyRunnable returning supplied hint. |
void |
shutdown() |
List<Runnable> |
shutdownNow() |
invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submitpublic PredictableExecutor(int buckets,
ThreadFactory threadFactory)
PredictableExecutor instance.buckets - number of buckets or 0 to match available processorsthreadFactory - ThreadFactory to use to create threadspublic PredictableExecutor(ThreadFactory threadFactory)
PredictableExecutor instance with
bucket size set to number of available processors.threadFactory - ThreadFactory to use to create threadspublic static PredictableExecutor newPredictableExecutor(int buckets, ThreadFactory threadFactory)
PredictableExecutor instance.buckets - number of buckets or 0 to match available processorsthreadFactory - ThreadFactory to use to create threadsPredictableExecutorprotected ExecutorService backendExecutorService(ThreadFactory threadFactory)
ExecutorService to use in the backend.threadFactory - ThreadFactory to use to create threadsExecutorServicepublic void execute(Runnable command, int hint)
command - the Runnable taskhint - value to pick thread to run on.public void execute(Runnable command, Function<Runnable,Integer> hintFunction)
command - the Runnable taskhintFunction - Function to compute hint valuepublic void shutdown()
shutdown in interface ExecutorServicepublic List<Runnable> shutdownNow()
shutdownNow in interface ExecutorServicepublic boolean isShutdown()
isShutdown in interface ExecutorServicepublic boolean isTerminated()
isTerminated in interface ExecutorServicepublic boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
Note: It'll try, but is not assured that the method will return by specified timeout.
awaitTermination in interface ExecutorServiceInterruptedExceptionprotected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(Callable<T> callable)
newTaskFor in class AbstractExecutorServiceprotected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(Runnable runnable, T value)
newTaskFor in class AbstractExecutorServicepublic static PredictableExecutor.PickyRunnable picky(Runnable runnable, int hint)
Runnable into PredictableExecutor.PickyRunnable returning supplied hint.runnable - Runnablehint - hint valuePredictableExecutor.PickyRunnablepublic static PredictableExecutor.PickyRunnable picky(Runnable runnable, Function<Runnable,Integer> hint)
Runnable into PredictableExecutor.PickyRunnable returning supplied hint.runnable - Runnablehint - hint functionPredictableExecutor.PickyRunnablepublic static <T> PredictableExecutor.PickyCallable<T> picky(Callable<T> callable, int hint)
Callable into PredictableExecutor.PickyCallable returning supplied hint.T - entity typecallable - Callablehint - hint valuePredictableExecutor.PickyCallablepublic static <T> PredictableExecutor.PickyCallable<T> picky(Callable<T> callable, Function<Callable<T>,Integer> hint)
Callable into PredictableExecutor.PickyCallable returning supplied hint.T - entity typecallable - Callablehint - hint functionPredictableExecutor.PickyCallable