public class PredictableExecutor
extends java.util.concurrent.AbstractExecutorService
implements java.util.concurrent.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,
java.util.concurrent.ThreadFactory threadFactory)
Creates
PredictableExecutor instance. |
PredictableExecutor(java.util.concurrent.ThreadFactory threadFactory)
Creates
PredictableExecutor instance with
bucket size set to number of available processors. |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitTermination(long timeout,
java.util.concurrent.TimeUnit unit) |
protected java.util.concurrent.ExecutorService |
backendExecutorService(java.util.concurrent.ThreadFactory threadFactory)
Creates a single thread
ExecutorService to use in the backend. |
void |
execute(java.lang.Runnable command) |
void |
execute(java.lang.Runnable command,
java.util.function.Function<java.lang.Runnable,java.lang.Integer> hintFunction)
Executes given command at some time in the future.
|
void |
execute(java.lang.Runnable command,
int hint)
Executes given command at some time in the future.
|
boolean |
isShutdown() |
boolean |
isTerminated() |
static PredictableExecutor |
newPredictableExecutor(int buckets,
java.util.concurrent.ThreadFactory threadFactory)
Creates
PredictableExecutor instance. |
protected <T> PredictableExecutor.PickyFutureTask<T> |
newTaskFor(java.util.concurrent.Callable<T> callable) |
protected <T> PredictableExecutor.PickyFutureTask<T> |
newTaskFor(java.lang.Runnable runnable,
T value) |
static <T> PredictableExecutor.PickyCallable<T> |
picky(java.util.concurrent.Callable<T> callable,
java.util.function.Function<java.util.concurrent.Callable<T>,java.lang.Integer> hint)
Wraps the given
Callable into PredictableExecutor.PickyCallable returning supplied hint. |
static <T> PredictableExecutor.PickyCallable<T> |
picky(java.util.concurrent.Callable<T> callable,
int hint)
Wraps the given
Callable into PredictableExecutor.PickyCallable returning supplied hint. |
static PredictableExecutor.PickyRunnable |
picky(java.lang.Runnable runnable,
java.util.function.Function<java.lang.Runnable,java.lang.Integer> hint)
Wraps the given
Runnable into PredictableExecutor.PickyRunnable returning supplied hint. |
static PredictableExecutor.PickyRunnable |
picky(java.lang.Runnable runnable,
int hint)
Wraps the given
Runnable into PredictableExecutor.PickyRunnable returning supplied hint. |
void |
shutdown() |
java.util.List<java.lang.Runnable> |
shutdownNow() |
invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submit
public PredictableExecutor(int buckets, java.util.concurrent.ThreadFactory threadFactory)
PredictableExecutor
instance.buckets
- number of buckets or 0 to match available processorsthreadFactory
- ThreadFactory
to use to create threadspublic PredictableExecutor(java.util.concurrent.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, java.util.concurrent.ThreadFactory threadFactory)
PredictableExecutor
instance.buckets
- number of buckets or 0 to match available processorsthreadFactory
- ThreadFactory
to use to create threadsPredictableExecutor
protected java.util.concurrent.ExecutorService backendExecutorService(java.util.concurrent.ThreadFactory threadFactory)
ExecutorService
to use in the backend.threadFactory
- ThreadFactory
to use to create threadsExecutorService
public void execute(java.lang.Runnable command, int hint)
command
- the Runnable
taskhint
- value to pick thread to run on.public void execute(java.lang.Runnable command, java.util.function.Function<java.lang.Runnable,java.lang.Integer> hintFunction)
command
- the Runnable
taskhintFunction
- Function to compute hint valuepublic void execute(java.lang.Runnable command)
execute
in interface java.util.concurrent.Executor
public void shutdown()
shutdown
in interface java.util.concurrent.ExecutorService
public java.util.List<java.lang.Runnable> shutdownNow()
shutdownNow
in interface java.util.concurrent.ExecutorService
public boolean isShutdown()
isShutdown
in interface java.util.concurrent.ExecutorService
public boolean isTerminated()
isTerminated
in interface java.util.concurrent.ExecutorService
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Note: It'll try, but is not assured that the method will return by specified timeout.
awaitTermination
in interface java.util.concurrent.ExecutorService
java.lang.InterruptedException
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(java.util.concurrent.Callable<T> callable)
newTaskFor
in class java.util.concurrent.AbstractExecutorService
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(java.lang.Runnable runnable, T value)
newTaskFor
in class java.util.concurrent.AbstractExecutorService
public static PredictableExecutor.PickyRunnable picky(java.lang.Runnable runnable, int hint)
Runnable
into PredictableExecutor.PickyRunnable
returning supplied hint.runnable
- Runnable
hint
- hint valuePredictableExecutor.PickyRunnable
public static PredictableExecutor.PickyRunnable picky(java.lang.Runnable runnable, java.util.function.Function<java.lang.Runnable,java.lang.Integer> hint)
Runnable
into PredictableExecutor.PickyRunnable
returning supplied hint.runnable
- Runnable
hint
- hint functionPredictableExecutor.PickyRunnable
public static <T> PredictableExecutor.PickyCallable<T> picky(java.util.concurrent.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(java.util.concurrent.Callable<T> callable, java.util.function.Function<java.util.concurrent.Callable<T>,java.lang.Integer> hint)
Callable
into PredictableExecutor.PickyCallable
returning supplied hint.T
- entity typecallable
- Callable
hint
- hint functionPredictableExecutor.PickyCallable