Class StripedExecutorService
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- eu.javaspecialists.tjsn.concurrency.stripedexecutor.StripedExecutorService
-
- All Implemented Interfaces:
java.util.concurrent.Executor,java.util.concurrent.ExecutorService
public class StripedExecutorService extends java.util.concurrent.AbstractExecutorServiceThe StripedExecutorService accepts Runnable/Callable objects that also implement the StripedObject interface. It executes all the tasks for a single "stripe" consecutively.In this version, submitted tasks do not necessarily have to implement the StripedObject interface. If they do not, then they will simply be passed onto the wrapped ExecutorService directly.
Idea inspired by Glenn McGregor on the Concurrency-interest mailing list and using the SerialExecutor presented in the Executor interface's JavaDocs.
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-
-
Constructor Summary
Constructors Constructor Description StripedExecutorService()This constructs a StripedExecutorService that wraps a cached thread pool.StripedExecutorService(int numberOfThreads)This constructs a StripedExecutorService that wraps a fixed thread pool with the given number of threads.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanawaitTermination(long timeout, java.util.concurrent.TimeUnit unit)Returns true if the wrapped ExecutorService terminates within the allotted amount of time.voidexecute(java.lang.Runnable command)Executes the command.booleanisShutdown()Returns true if shutdown() or shutdownNow() have been called; false otherwise.booleanisTerminated()Returns true if this pool has been terminated, that is, all the SerialExecutors are empty and the wrapped ExecutorService has been terminated.protected <T> java.util.concurrent.RunnableFuture<T>newTaskFor(java.lang.Runnable runnable, T value)If the runnable also implements StripedObject, we store the stripe object in a thread local, since the actual runnable will be wrapped with a FutureTask.protected <T> java.util.concurrent.RunnableFuture<T>newTaskFor(java.util.concurrent.Callable<T> callable)If the callable also implements StripedObject, we store the stripe object in a thread local, since the actual callable will be wrapped with a FutureTask.voidshutdown()Shuts down the StripedExecutorService.java.util.List<java.lang.Runnable>shutdownNow()All the tasks in each of the SerialExecutors are drained to a list, as well as the tasks inside the wrapped ExecutorService.java.util.concurrent.Future<?>submit(java.lang.Runnable task)Delegates the call to submit(task, null).<T> java.util.concurrent.Future<T>submit(java.lang.Runnable task, T result)If the task is a StripedObject, we execute it in-order by its stripe, otherwise we submit it directly to the wrapped executor.<T> java.util.concurrent.Future<T>submit(java.util.concurrent.Callable<T> task)If the task is a StripedObject, we execute it in-order by its stripe, otherwise we submit it directly to the wrapped executor.java.lang.StringtoString()Prints information about current state of this executor, the wrapped executor and the serial executors.
-
-
-
Constructor Detail
-
StripedExecutorService
public StripedExecutorService()
This constructs a StripedExecutorService that wraps a cached thread pool.
-
StripedExecutorService
public StripedExecutorService(int numberOfThreads)
This constructs a StripedExecutorService that wraps a fixed thread pool with the given number of threads.- Parameters:
numberOfThreads- the number of threads
-
-
Method Detail
-
newTaskFor
protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor(java.lang.Runnable runnable, T value)If the runnable also implements StripedObject, we store the stripe object in a thread local, since the actual runnable will be wrapped with a FutureTask.- Overrides:
newTaskForin classjava.util.concurrent.AbstractExecutorService
-
newTaskFor
protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor(java.util.concurrent.Callable<T> callable)
If the callable also implements StripedObject, we store the stripe object in a thread local, since the actual callable will be wrapped with a FutureTask.- Overrides:
newTaskForin classjava.util.concurrent.AbstractExecutorService
-
submit
public java.util.concurrent.Future<?> submit(java.lang.Runnable task)
Delegates the call to submit(task, null).- Specified by:
submitin interfacejava.util.concurrent.ExecutorService- Overrides:
submitin classjava.util.concurrent.AbstractExecutorService
-
submit
public <T> java.util.concurrent.Future<T> submit(java.lang.Runnable task, T result)If the task is a StripedObject, we execute it in-order by its stripe, otherwise we submit it directly to the wrapped executor. If the pool is not running, we throw a RejectedExecutionException.- Specified by:
submitin interfacejava.util.concurrent.ExecutorService- Overrides:
submitin classjava.util.concurrent.AbstractExecutorService
-
submit
public <T> java.util.concurrent.Future<T> submit(java.util.concurrent.Callable<T> task)
If the task is a StripedObject, we execute it in-order by its stripe, otherwise we submit it directly to the wrapped executor. If the pool is not running, we throw a RejectedExecutionException.- Specified by:
submitin interfacejava.util.concurrent.ExecutorService- Overrides:
submitin classjava.util.concurrent.AbstractExecutorService
-
execute
public void execute(java.lang.Runnable command)
Executes the command. If command implements StripedObject, we execute it with a SerialExecutor. This method can be called directly by clients or it may be called by the AbstractExecutorService's submit() methods. In that case, we check whether the stripes thread local has been set. If it is, we remove it and use it to determine the StripedObject and execute it with a SerialExecutor. If no StripedObject is set, we instead pass the command to the wrapped ExecutorService directly.
-
shutdown
public void shutdown()
Shuts down the StripedExecutorService. No more tasks will be submitted. If the map of SerialExecutors is empty, we shut down the wrapped executor.
-
shutdownNow
public java.util.List<java.lang.Runnable> shutdownNow()
All the tasks in each of the SerialExecutors are drained to a list, as well as the tasks inside the wrapped ExecutorService. This is then returned to the user. Also, the shutdownNow method of the wrapped executor is called.
-
isShutdown
public boolean isShutdown()
Returns true if shutdown() or shutdownNow() have been called; false otherwise.
-
isTerminated
public boolean isTerminated()
Returns true if this pool has been terminated, that is, all the SerialExecutors are empty and the wrapped ExecutorService has been terminated.
-
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionReturns true if the wrapped ExecutorService terminates within the allotted amount of time.- Throws:
java.lang.InterruptedException
-
toString
public java.lang.String toString()
Prints information about current state of this executor, the wrapped executor and the serial executors.- Overrides:
toStringin classjava.lang.Object
-
-