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 interfacePredictableExecutor.PickyCallable<T>Callablealso implementingPredictableExecutor.PickyTask.static classPredictableExecutor.PickyFutureTask<T>AFutureTaskimplementingPredictableExecutor.PickyTask.static interfacePredictableExecutor.PickyRunnableRunnablealso implementingPredictableExecutor.PickyTask.static interfacePredictableExecutor.PickyTaskAbstraction 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)CreatesPredictableExecutorinstance.PredictableExecutor(int buckets, ThreadFactory threadFactory, boolean directExec)CreatesPredictableExecutorinstance.PredictableExecutor(ThreadFactory threadFactory)CreatesPredictableExecutorinstance with bucket size set to number of available processors. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanawaitTermination(long timeout, TimeUnit unit)protected ExecutorServicebackendExecutorService(ThreadFactory threadFactory, boolean direct)Creates a single threadExecutorServiceto use in the backend.voidexecute(Runnable command)voidexecute(Runnable command, int hint)Executes given command at some time in the future.voidexecute(Runnable command, Function<Runnable,Integer> hintFunction)Executes given command at some time in the future.booleanisShutdown()booleanisTerminated()static PredictableExecutornewPredictableExecutor(int buckets, ThreadFactory threadFactory)CreatesPredictableExecutorinstance.protected <T> PredictableExecutor.PickyFutureTask<T>newTaskFor(Runnable runnable, T value)protected <T> PredictableExecutor.PickyFutureTask<T>newTaskFor(Callable<T> callable)static PredictableExecutor.PickyRunnablepicky(Runnable runnable, int hint)Wraps the givenRunnableintoPredictableExecutor.PickyRunnablereturning supplied hint.static PredictableExecutor.PickyRunnablepicky(Runnable runnable, Function<Runnable,Integer> hint)Wraps the givenRunnableintoPredictableExecutor.PickyRunnablereturning supplied hint.static <T> PredictableExecutor.PickyCallable<T>picky(Callable<T> callable, int hint)Wraps the givenCallableintoPredictableExecutor.PickyCallablereturning supplied hint.static <T> PredictableExecutor.PickyCallable<T>picky(Callable<T> callable, Function<Callable<T>,Integer> hint)Wraps the givenCallableintoPredictableExecutor.PickyCallablereturning supplied hint.voidshutdown()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)CreatesPredictableExecutorinstance.- Parameters:
 buckets- number of buckets or 0 to match available processorsthreadFactory-ThreadFactoryto use to create threads
 
- 
PredictableExecutor
public PredictableExecutor(int buckets, ThreadFactory threadFactory, boolean directExec)CreatesPredictableExecutorinstance. Meant for testing purposes.- Parameters:
 buckets- number of buckets or 0 to match available processorsthreadFactory-ThreadFactoryto use to create threadsdirectExec- direct executors
 
- 
PredictableExecutor
public PredictableExecutor(ThreadFactory threadFactory)
CreatesPredictableExecutorinstance with bucket size set to number of available processors.- Parameters:
 threadFactory-ThreadFactoryto use to create threads
 
 - 
 
- 
Method Detail
- 
newPredictableExecutor
public static PredictableExecutor newPredictableExecutor(int buckets, ThreadFactory threadFactory)
CreatesPredictableExecutorinstance.- Parameters:
 buckets- number of buckets or 0 to match available processorsthreadFactory-ThreadFactoryto use to create threads- Returns:
 PredictableExecutor
 
- 
backendExecutorService
protected ExecutorService backendExecutorService(ThreadFactory threadFactory, boolean direct)
Creates a single threadExecutorServiceto use in the backend.- Parameters:
 threadFactory-ThreadFactoryto use to create threadsdirect- direct executors- Returns:
 - single thread 
ExecutorServiceor direct executor 
 
- 
execute
public void execute(Runnable command, int hint)
Executes given command at some time in the future.- Parameters:
 command- theRunnabletaskhint- 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- theRunnabletaskhintFunction- 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'sgetmethod will return the task's result upon successful completion.- Parameters:
 command- theRunnabletaskhint- 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'sgetmethod will return the task's result upon successful completion.- Parameters:
 command- theRunnabletaskhintFunction- Function to compute hint value- Returns:
 - completable future representing the pending results
 
 
- 
shutdown
public void shutdown()
- Specified by:
 shutdownin interfaceExecutorService
 
- 
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
 shutdownNowin interfaceExecutorService
 
- 
isShutdown
public boolean isShutdown()
- Specified by:
 isShutdownin interfaceExecutorService
 
- 
isTerminated
public boolean isTerminated()
- Specified by:
 isTerminatedin interfaceExecutorService
 
- 
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedExceptionNote: It'll try, but is not assured that the method will return by specified timeout.
- Specified by:
 awaitTerminationin interfaceExecutorService- Throws:
 InterruptedException
 
- 
newTaskFor
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(Callable<T> callable)
- Overrides:
 newTaskForin classAbstractExecutorService
 
- 
newTaskFor
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(Runnable runnable, T value)
- Overrides:
 newTaskForin classAbstractExecutorService
 
- 
picky
public static PredictableExecutor.PickyRunnable picky(Runnable runnable, int hint)
Wraps the givenRunnableintoPredictableExecutor.PickyRunnablereturning supplied hint.- Parameters:
 runnable-Runnablehint- hint value- Returns:
 PredictableExecutor.PickyRunnable
 
- 
picky
public static PredictableExecutor.PickyRunnable picky(Runnable runnable, Function<Runnable,Integer> hint)
Wraps the givenRunnableintoPredictableExecutor.PickyRunnablereturning supplied hint.- Parameters:
 runnable-Runnablehint- hint function- Returns:
 PredictableExecutor.PickyRunnable
 
- 
picky
public static <T> PredictableExecutor.PickyCallable<T> picky(Callable<T> callable, int hint)
Wraps the givenCallableintoPredictableExecutor.PickyCallablereturning supplied hint.- Type Parameters:
 T- entity type- Parameters:
 callable-Callablehint- hint value- Returns:
 PredictableExecutor.PickyCallable
 
- 
picky
public static <T> PredictableExecutor.PickyCallable<T> picky(Callable<T> callable, Function<Callable<T>,Integer> hint)
Wraps the givenCallableintoPredictableExecutor.PickyCallablereturning supplied hint.- Type Parameters:
 T- entity type- Parameters:
 callable-Callablehint- hint function- Returns:
 PredictableExecutor.PickyCallable
 
 - 
 
 -