public class DeadlockDetectingListeningExecutorService extends AsyncNotifyingListeningExecutorService
get
methods synchronously.
Deadlock scenarios are most apt to occur with a backing single-threaded executor where setting of the Future's result is executed on the single thread. Here's a scenario:
get()
synchronously on the returned FutureThis class prevents this scenario via the use of a ThreadLocal variable. When a task is invoked,
the ThreadLocal is set and, when a task completes, the ThreadLocal is cleared. Futures returned
from this class override the get
methods to check if the ThreadLocal is set. If it is,
an ExecutionException is thrown with a custom cause.
Note that the ThreadLocal is not removed automatically, so some state may be left hanging off of
threads which have encountered this class. If you need to clean that state up, use
cleanStateForCurrentThread()
.
Constructor and Description |
---|
DeadlockDetectingListeningExecutorService(ExecutorService delegate,
Supplier<Exception> deadlockExceptionSupplier)
Constructor.
|
DeadlockDetectingListeningExecutorService(ExecutorService delegate,
Supplier<Exception> deadlockExceptionSupplier,
Executor listenableFutureExecutor)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
cleanStateForCurrentThread()
Remove the state this instance may have attached to the calling thread.
|
void |
execute(Runnable command) |
<T> ListenableFuture<T> |
submit(Callable<T> task) |
ListenableFuture<?> |
submit(Runnable task) |
<T> ListenableFuture<T> |
submit(Runnable task,
T result) |
addToStringAttributes, awaitTermination, getDelegate, isShutdown, isTerminated, shutdown, shutdownNow, toString
newTaskFor, newTaskFor
invokeAll, invokeAll, invokeAny, invokeAny
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
invokeAll, invokeAll
invokeAny, invokeAny
public DeadlockDetectingListeningExecutorService(ExecutorService delegate, @Nonnull Supplier<Exception> deadlockExceptionSupplier)
delegate
- the backing ExecutorService.deadlockExceptionSupplier
- Supplier that returns an Exception instance to set as the
cause of the ExecutionException when a deadlock is detected.public DeadlockDetectingListeningExecutorService(ExecutorService delegate, @Nonnull Supplier<Exception> deadlockExceptionSupplier, @Nullable Executor listenableFutureExecutor)
delegate
- the backing ExecutorService.deadlockExceptionSupplier
- Supplier that returns an Exception instance to set as the
cause of the ExecutionException when a deadlock is detected.listenableFutureExecutor
- the executor used to run listener callbacks asynchronously.
If null, no executor is used.public void execute(@Nonnull Runnable command)
execute
in interface Executor
execute
in class AsyncNotifyingListeningExecutorService
@Nonnull public <T> ListenableFuture<T> submit(Callable<T> task)
submit
in interface ListeningExecutorService
submit
in interface ExecutorService
submit
in class AsyncNotifyingListeningExecutorService
@Nonnull public ListenableFuture<?> submit(Runnable task)
submit
in interface ListeningExecutorService
submit
in interface ExecutorService
submit
in class AsyncNotifyingListeningExecutorService
@Nonnull public <T> ListenableFuture<T> submit(Runnable task, T result)
submit
in interface ListeningExecutorService
submit
in interface ExecutorService
submit
in class AsyncNotifyingListeningExecutorService
public void cleanStateForCurrentThread()
Copyright © 2018 OpenDaylight. All rights reserved.