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, submit
public 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 threadsPredictableExecutor
protected ExecutorService backendExecutorService(ThreadFactory threadFactory)
ExecutorService
to use in the backend.threadFactory
- ThreadFactory
to use to create threadsExecutorService
public 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 ExecutorService
public List<Runnable> shutdownNow()
shutdownNow
in interface ExecutorService
public boolean isShutdown()
isShutdown
in interface ExecutorService
public boolean isTerminated()
isTerminated
in interface ExecutorService
public 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 ExecutorService
InterruptedException
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(Callable<T> callable)
newTaskFor
in class AbstractExecutorService
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(Runnable runnable, T value)
newTaskFor
in class AbstractExecutorService
public static PredictableExecutor.PickyRunnable picky(Runnable runnable, int hint)
Runnable
into PredictableExecutor.PickyRunnable
returning supplied hint.runnable
- Runnable
hint
- hint valuePredictableExecutor.PickyRunnable
public static PredictableExecutor.PickyRunnable picky(Runnable runnable, Function<Runnable,Integer> hint)
Runnable
into PredictableExecutor.PickyRunnable
returning supplied hint.runnable
- Runnable
hint
- hint functionPredictableExecutor.PickyRunnable
public static <T> PredictableExecutor.PickyCallable<T> picky(Callable<T> callable, int hint)
Callable
into PredictableExecutor.PickyCallable
returning supplied hint.T
- entity typecallable
- Callable
hint
- hint valuePredictableExecutor.PickyCallable
public static <T> PredictableExecutor.PickyCallable<T> picky(Callable<T> callable, Function<Callable<T>,Integer> hint)
Callable
into PredictableExecutor.PickyCallable
returning supplied hint.T
- entity typecallable
- Callable
hint
- hint functionPredictableExecutor.PickyCallable