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 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, java.util.concurrent.ThreadFactory threadFactory)CreatesPredictableExecutorinstance.PredictableExecutor(int buckets, java.util.concurrent.ThreadFactory threadFactory, boolean directExec)CreatesPredictableExecutorinstance.PredictableExecutor(java.util.concurrent.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, java.util.concurrent.TimeUnit unit)protected java.util.concurrent.ExecutorServicebackendExecutorService(java.util.concurrent.ThreadFactory threadFactory, boolean direct)Creates a single threadExecutorServiceto use in the backend.voidexecute(java.lang.Runnable command)voidexecute(java.lang.Runnable command, int hint)Executes given command at some time in the future.voidexecute(java.lang.Runnable command, java.util.function.Function<java.lang.Runnable,java.lang.Integer> hintFunction)Executes given command at some time in the future.booleanisShutdown()booleanisTerminated()static PredictableExecutornewPredictableExecutor(int buckets, java.util.concurrent.ThreadFactory threadFactory)CreatesPredictableExecutorinstance.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.PickyRunnablepicky(java.lang.Runnable runnable, int hint)Wraps the givenRunnableintoPredictableExecutor.PickyRunnablereturning supplied hint.static PredictableExecutor.PickyRunnablepicky(java.lang.Runnable runnable, java.util.function.Function<java.lang.Runnable,java.lang.Integer> hint)Wraps the givenRunnableintoPredictableExecutor.PickyRunnablereturning supplied hint.static <T> PredictableExecutor.PickyCallable<T>picky(java.util.concurrent.Callable<T> callable, int hint)Wraps the givenCallableintoPredictableExecutor.PickyCallablereturning 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 givenCallableintoPredictableExecutor.PickyCallablereturning supplied hint.voidshutdown()java.util.List<java.lang.Runnable>shutdownNow()-
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)CreatesPredictableExecutorinstance.- Parameters:
buckets- number of buckets or 0 to match available processorsthreadFactory-ThreadFactoryto use to create threads
-
PredictableExecutor
public PredictableExecutor(int buckets, java.util.concurrent.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(java.util.concurrent.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, java.util.concurrent.ThreadFactory threadFactory)
CreatesPredictableExecutorinstance.- Parameters:
buckets- number of buckets or 0 to match available processorsthreadFactory-ThreadFactoryto use to create threads- Returns:
PredictableExecutor
-
backendExecutorService
protected java.util.concurrent.ExecutorService backendExecutorService(java.util.concurrent.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(java.lang.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(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- theRunnabletaskhintFunction- Function to compute hint value
-
execute
public void execute(java.lang.Runnable command)
- Specified by:
executein interfacejava.util.concurrent.Executor
-
shutdown
public void shutdown()
- Specified by:
shutdownin interfacejava.util.concurrent.ExecutorService
-
shutdownNow
public java.util.List<java.lang.Runnable> shutdownNow()
- Specified by:
shutdownNowin interfacejava.util.concurrent.ExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdownin interfacejava.util.concurrent.ExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminatedin interfacejava.util.concurrent.ExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionNote: It'll try, but is not assured that the method will return by specified timeout.
- Specified by:
awaitTerminationin interfacejava.util.concurrent.ExecutorService- Throws:
java.lang.InterruptedException
-
newTaskFor
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(java.util.concurrent.Callable<T> callable)
- Overrides:
newTaskForin classjava.util.concurrent.AbstractExecutorService
-
newTaskFor
protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor(java.lang.Runnable runnable, T value)
- Overrides:
newTaskForin classjava.util.concurrent.AbstractExecutorService
-
picky
public static PredictableExecutor.PickyRunnable picky(java.lang.Runnable runnable, int hint)
Wraps the givenRunnableintoPredictableExecutor.PickyRunnablereturning supplied hint.- Parameters:
runnable-Runnablehint- 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 givenRunnableintoPredictableExecutor.PickyRunnablereturning supplied hint.- Parameters:
runnable-Runnablehint- hint function- Returns:
PredictableExecutor.PickyRunnable
-
picky
public static <T> PredictableExecutor.PickyCallable<T> picky(java.util.concurrent.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(java.util.concurrent.Callable<T> callable, java.util.function.Function<java.util.concurrent.Callable<T>,java.lang.Integer> hint)
Wraps the givenCallableintoPredictableExecutor.PickyCallablereturning supplied hint.- Type Parameters:
T- entity type- Parameters:
callable-Callablehint- hint function- Returns:
PredictableExecutor.PickyCallable
-
-