Package org.onlab.util
Class PredictableExecutor
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- org.onlab.util.PredictableExecutor
-
- All Implemented Interfaces:
Executor
,ExecutorService
public class PredictableExecutor extends AbstractExecutorService implements ExecutorService
(Somewhat) predictable ExecutorService.ExecutorService which behaves similar to the one created by
Executors.newFixedThreadPool(int, ThreadFactory)
, but assigns command to specific thread based on it'sPredictableExecutor.PickyTask.hint()
,Object.hashCode()
, or hint value explicitly specified when the command was passed to thisPredictableExecutor
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
PredictableExecutor.PickyCallable<T>
Callable
also implementingPredictableExecutor.PickyTask
.static class
PredictableExecutor.PickyFutureTask<T>
AFutureTask
implementingPredictableExecutor.PickyTask
.static interface
PredictableExecutor.PickyRunnable
Runnable
also implementingPredictableExecutor.PickyTask
.static interface
PredictableExecutor.PickyTask
Abstraction to give a task a way to express it's preference to run on certain thread.
-
Constructor Summary
Constructors Constructor Description PredictableExecutor(int buckets, ThreadFactory threadFactory)
CreatesPredictableExecutor
instance.PredictableExecutor(int buckets, ThreadFactory threadFactory, boolean directExec)
CreatesPredictableExecutor
instance.PredictableExecutor(ThreadFactory threadFactory)
CreatesPredictableExecutor
instance with bucket size set to number of available processors.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
awaitTermination(long timeout, TimeUnit unit)
protected ExecutorService
backendExecutorService(ThreadFactory threadFactory, boolean direct)
Creates a single threadExecutorService
to use in the backend.void
execute(Runnable command)
void
execute(Runnable command, int hint)
Executes given command at some time in the future.void
execute(Runnable command, Function<Runnable,Integer> hintFunction)
Executes given command at some time in the future.boolean
isShutdown()
boolean
isTerminated()
static PredictableExecutor
newPredictableExecutor(int buckets, ThreadFactory threadFactory)
CreatesPredictableExecutor
instance.protected <T> PredictableExecutor.PickyFutureTask<T>
newTaskFor(Runnable runnable, T value)
protected <T> PredictableExecutor.PickyFutureTask<T>
newTaskFor(Callable<T> callable)
static PredictableExecutor.PickyRunnable
picky(Runnable runnable, int hint)
Wraps the givenRunnable
intoPredictableExecutor.PickyRunnable
returning supplied hint.static PredictableExecutor.PickyRunnable
picky(Runnable runnable, Function<Runnable,Integer> hint)
Wraps the givenRunnable
intoPredictableExecutor.PickyRunnable
returning supplied hint.static <T> PredictableExecutor.PickyCallable<T>
picky(Callable<T> callable, int hint)
Wraps the givenCallable
intoPredictableExecutor.PickyCallable
returning supplied hint.static <T> PredictableExecutor.PickyCallable<T>
picky(Callable<T> callable, Function<Callable<T>,Integer> hint)
Wraps the givenCallable
intoPredictableExecutor.PickyCallable
returning supplied hint.void
shutdown()
List<Runnable>
shutdownNow()
CompletableFuture<Void>
submit(Runnable command, int hint)
Submits a value-returning task for execution and returns a Future representing the pending results of the task.CompletableFuture<Void>
submit(Runnable command, Function<Runnable,Integer> hintFunction)
Submits a value-returning task for execution and returns a Future representing the pending results of the task.-
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submit
-
-
-
-
Constructor Detail
-
PredictableExecutor
public PredictableExecutor(int buckets, ThreadFactory threadFactory)
CreatesPredictableExecutor
instance.- Parameters:
buckets
- number of buckets or 0 to match available processorsthreadFactory
-ThreadFactory
to use to create threads
-
PredictableExecutor
public PredictableExecutor(int buckets, ThreadFactory threadFactory, boolean directExec)
CreatesPredictableExecutor
instance. Meant for testing purposes.- Parameters:
buckets
- number of buckets or 0 to match available processorsthreadFactory
-ThreadFactory
to use to create threadsdirectExec
- direct executors
-
PredictableExecutor
public PredictableExecutor(ThreadFactory threadFactory)
CreatesPredictableExecutor
instance with bucket size set to number of available processors.- Parameters:
threadFactory
-ThreadFactory
to use to create threads
-
-
Method Detail
-
newPredictableExecutor
public static PredictableExecutor newPredictableExecutor(int buckets, ThreadFactory threadFactory)
CreatesPredictableExecutor
instance.- Parameters:
buckets
- number of buckets or 0 to match available processorsthreadFactory
-ThreadFactory
to use to create threads- Returns:
PredictableExecutor
-
backendExecutorService
protected ExecutorService backendExecutorService(ThreadFactory threadFactory, boolean direct)
Creates a single threadExecutorService
to use in the backend.- Parameters:
threadFactory
-ThreadFactory
to use to create threadsdirect
- direct executors- Returns:
- single thread
ExecutorService
or direct executor
-
execute
public void execute(Runnable command, int hint)
Executes given command at some time in the future.- Parameters:
command
- theRunnable
taskhint
- value to pick thread to run on.
-
execute
public void execute(Runnable command, Function<Runnable,Integer> hintFunction)
Executes given command at some time in the future.- Parameters:
command
- theRunnable
taskhintFunction
- Function to compute hint value
-
submit
public CompletableFuture<Void> submit(Runnable command, int hint)
Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future'sget
method will return the task's result upon successful completion.- Parameters:
command
- theRunnable
taskhint
- value to pick thread to run on.- Returns:
- completable future representing the pending results
-
submit
public CompletableFuture<Void> submit(Runnable command, Function<Runnable,Integer> hintFunction)
Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future'sget
method will return the task's result upon successful completion.- Parameters:
command
- theRunnable
taskhintFunction
- Function to compute hint value- Returns:
- completable future representing the pending results
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNow
in interfaceExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown
in interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated
in interfaceExecutorService
-
awaitTermination
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.
- Specified by:
awaitTermination
in interfaceExecutorService
- Throws:
InterruptedException
-
newTaskFor
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(Callable<T> callable)
- Overrides:
newTaskFor
in classAbstractExecutorService
-
newTaskFor
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(Runnable runnable, T value)
- Overrides:
newTaskFor
in classAbstractExecutorService
-
picky
public static PredictableExecutor.PickyRunnable picky(Runnable runnable, int hint)
Wraps the givenRunnable
intoPredictableExecutor.PickyRunnable
returning supplied hint.- Parameters:
runnable
-Runnable
hint
- hint value- Returns:
PredictableExecutor.PickyRunnable
-
picky
public static PredictableExecutor.PickyRunnable picky(Runnable runnable, Function<Runnable,Integer> hint)
Wraps the givenRunnable
intoPredictableExecutor.PickyRunnable
returning supplied hint.- Parameters:
runnable
-Runnable
hint
- hint function- Returns:
PredictableExecutor.PickyRunnable
-
picky
public static <T> PredictableExecutor.PickyCallable<T> picky(Callable<T> callable, int hint)
Wraps the givenCallable
intoPredictableExecutor.PickyCallable
returning supplied hint.- Type Parameters:
T
- entity type- Parameters:
callable
-Callable
hint
- hint value- Returns:
PredictableExecutor.PickyCallable
-
picky
public static <T> PredictableExecutor.PickyCallable<T> picky(Callable<T> callable, Function<Callable<T>,Integer> hint)
Wraps the givenCallable
intoPredictableExecutor.PickyCallable
returning supplied hint.- Type Parameters:
T
- entity type- Parameters:
callable
-Callable
hint
- hint function- Returns:
PredictableExecutor.PickyCallable
-
-