Package org.onlab.util
Class PredictableExecutor
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- org.onlab.util.PredictableExecutor
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,java.util.concurrent.ExecutorService
public class PredictableExecutor extends java.util.concurrent.AbstractExecutorService implements java.util.concurrent.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, java.util.concurrent.ThreadFactory threadFactory)
CreatesPredictableExecutor
instance.PredictableExecutor(int buckets, java.util.concurrent.ThreadFactory threadFactory, boolean directExec)
CreatesPredictableExecutor
instance.PredictableExecutor(java.util.concurrent.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, java.util.concurrent.TimeUnit unit)
protected java.util.concurrent.ExecutorService
backendExecutorService(java.util.concurrent.ThreadFactory threadFactory, boolean direct)
Creates a single threadExecutorService
to use in the backend.void
execute(java.lang.Runnable command)
void
execute(java.lang.Runnable command, int hint)
Executes given command at some time in the future.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.boolean
isShutdown()
boolean
isTerminated()
static PredictableExecutor
newPredictableExecutor(int buckets, java.util.concurrent.ThreadFactory threadFactory)
CreatesPredictableExecutor
instance.protected <T> PredictableExecutor.PickyFutureTask<T>
newTaskFor(java.lang.Runnable runnable, T value)
protected <T> PredictableExecutor.PickyFutureTask<T>
newTaskFor(java.util.concurrent.Callable<T> callable)
static PredictableExecutor.PickyRunnable
picky(java.lang.Runnable runnable, int hint)
Wraps the givenRunnable
intoPredictableExecutor.PickyRunnable
returning supplied hint.static PredictableExecutor.PickyRunnable
picky(java.lang.Runnable runnable, java.util.function.Function<java.lang.Runnable,java.lang.Integer> hint)
Wraps the givenRunnable
intoPredictableExecutor.PickyRunnable
returning supplied hint.static <T> PredictableExecutor.PickyCallable<T>
picky(java.util.concurrent.Callable<T> callable, int hint)
Wraps the givenCallable
intoPredictableExecutor.PickyCallable
returning supplied hint.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 givenCallable
intoPredictableExecutor.PickyCallable
returning supplied hint.void
shutdown()
java.util.List<java.lang.Runnable>
shutdownNow()
java.util.concurrent.CompletableFuture<java.lang.Void>
submit(java.lang.Runnable command, int hint)
Submits a value-returning task for execution and returns a Future representing the pending results of the task.java.util.concurrent.CompletableFuture<java.lang.Void>
submit(java.lang.Runnable command, java.util.function.Function<java.lang.Runnable,java.lang.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, java.util.concurrent.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, java.util.concurrent.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(java.util.concurrent.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, java.util.concurrent.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 java.util.concurrent.ExecutorService backendExecutorService(java.util.concurrent.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(java.lang.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(java.lang.Runnable command, java.util.function.Function<java.lang.Runnable,java.lang.Integer> hintFunction)
Executes given command at some time in the future.- Parameters:
command
- theRunnable
taskhintFunction
- Function to compute hint value
-
submit
public java.util.concurrent.CompletableFuture<java.lang.Void> submit(java.lang.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 java.util.concurrent.CompletableFuture<java.lang.Void> submit(java.lang.Runnable command, java.util.function.Function<java.lang.Runnable,java.lang.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
-
execute
public void execute(java.lang.Runnable command)
- Specified by:
execute
in interfacejava.util.concurrent.Executor
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfacejava.util.concurrent.ExecutorService
-
shutdownNow
public java.util.List<java.lang.Runnable> shutdownNow()
- Specified by:
shutdownNow
in interfacejava.util.concurrent.ExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown
in interfacejava.util.concurrent.ExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated
in interfacejava.util.concurrent.ExecutorService
-
awaitTermination
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.
- Specified by:
awaitTermination
in interfacejava.util.concurrent.ExecutorService
- Throws:
java.lang.InterruptedException
-
newTaskFor
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(java.util.concurrent.Callable<T> callable)
- Overrides:
newTaskFor
in classjava.util.concurrent.AbstractExecutorService
-
newTaskFor
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(java.lang.Runnable runnable, T value)
- Overrides:
newTaskFor
in classjava.util.concurrent.AbstractExecutorService
-
picky
public static PredictableExecutor.PickyRunnable picky(java.lang.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(java.lang.Runnable runnable, java.util.function.Function<java.lang.Runnable,java.lang.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(java.util.concurrent.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(java.util.concurrent.Callable<T> callable, java.util.function.Function<java.util.concurrent.Callable<T>,java.lang.Integer> hint)
Wraps the givenCallable
intoPredictableExecutor.PickyCallable
returning supplied hint.- Type Parameters:
T
- entity type- Parameters:
callable
-Callable
hint
- hint function- Returns:
PredictableExecutor.PickyCallable
-
-