Package pl.morgwai.base.guice.scopes
Class ContextTrackingExecutor
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- java.util.concurrent.ThreadPoolExecutor
-
- pl.morgwai.base.guice.scopes.ContextTrackingExecutor
-
- All Implemented Interfaces:
Executor
,ExecutorService
public class ContextTrackingExecutor extends ThreadPoolExecutor
AThreadPoolExecutor
that upon task execution automatically updates which thread runs within whichServerSideContext
using suppliedtrackers
.Instances usually correspond 1-1 with some type of blocking or time consuming operations, such as CPU/GPU intensive calculations or blocking network communication with some resource.
In case of network operations, a given threadPool size should usually correspond to the pool size of the connections to the given resource.
In case of CPU/GPU intensive operations, it should usually correspond to the number of given cores available to the process.Instances are usually created at app startup, stored on static vars and/or configured for injection using
bind(ContextTrackingExecutor.class) .annotatedWith(Names.named("someOpTypeExecutor")) .toInstance(...)
and injected with@Named("someOpTypeExecutor") ContextTrackingExecutor someOpTypeExecutor
If multiple threads run within the same context (for example by using
invokeAll(Collection)
), then the attributes they access must be thread-safe or properly synchronized.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
-
-
Constructor Summary
Constructors Constructor Description ContextTrackingExecutor(String name, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler, ContextTracker<?>... trackers)
ContextTrackingExecutor(String name, int poolSize, BlockingQueue<Runnable> workQueue, ContextTracker<?>... trackers)
ContextTrackingExecutor(String name, int poolSize, ContextTracker<?>... trackers)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
execute(Runnable task)
String
getName()
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks)
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
<T> T
invokeAny(Collection<? extends Callable<T>> tasks)
<T> T
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
Future<?>
submit(Runnable task)
<T> Future<T>
submit(Runnable task, T result)
<T> Future<T>
submit(Callable<T> task)
List<Runnable>
tryShutdownGracefully(long timeoutSeconds)
CallsThreadPoolExecutor.shutdown()
and waitstimeoutSeconds
for termination.-
Methods inherited from class java.util.concurrent.ThreadPoolExecutor
afterExecute, allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, terminated, toString
-
Methods inherited from class java.util.concurrent.AbstractExecutorService
newTaskFor, newTaskFor
-
-
-
-
Constructor Detail
-
ContextTrackingExecutor
public ContextTrackingExecutor(String name, int poolSize, ContextTracker<?>... trackers)
-
ContextTrackingExecutor
public ContextTrackingExecutor(String name, int poolSize, BlockingQueue<Runnable> workQueue, ContextTracker<?>... trackers)
-
ContextTrackingExecutor
public ContextTrackingExecutor(String name, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler, ContextTracker<?>... trackers)
-
-
Method Detail
-
getName
public String getName()
-
execute
public void execute(Runnable task)
- Specified by:
execute
in interfaceExecutor
- Overrides:
execute
in classThreadPoolExecutor
-
submit
public <T> Future<T> submit(Callable<T> task)
- Specified by:
submit
in interfaceExecutorService
- Overrides:
submit
in classAbstractExecutorService
-
submit
public <T> Future<T> submit(Runnable task, T result)
- Specified by:
submit
in interfaceExecutorService
- Overrides:
submit
in classAbstractExecutorService
-
submit
public Future<?> submit(Runnable task)
- Specified by:
submit
in interfaceExecutorService
- Overrides:
submit
in classAbstractExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Overrides:
invokeAll
in classAbstractExecutorService
- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Overrides:
invokeAll
in classAbstractExecutorService
- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
- Specified by:
invokeAny
in interfaceExecutorService
- Overrides:
invokeAny
in classAbstractExecutorService
- Throws:
InterruptedException
ExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
invokeAny
in interfaceExecutorService
- Overrides:
invokeAny
in classAbstractExecutorService
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
tryShutdownGracefully
public List<Runnable> tryShutdownGracefully(long timeoutSeconds)
CallsThreadPoolExecutor.shutdown()
and waitstimeoutSeconds
for termination. If it fails, callsThreadPoolExecutor.shutdownNow()
. Logs outcome toLogger
named after this class.- Returns:
null
if the executor was shutdown cleanly, list of tasks returned byThreadPoolExecutor.shutdownNow()
otherwise.
-
-