Class CancelDebuggingListenableFuture<T>
- java.lang.Object
-
- org.threadly.concurrent.future.CancelDebuggingListenableFuture<T>
-
- Type Parameters:
T- Type of result provided by this ListenableFuture
- All Implemented Interfaces:
java.util.concurrent.Future<T>,ListenableFuture<T>
public class CancelDebuggingListenableFuture<T> extends java.lang.Object implements ListenableFuture<T>
Wrapper for aListenableFutureto provide enhanced features for debugging the state at which at which it was canceled. When a cancel request comes in this class will attempt to record the stack trace of the delegateFuture. If it then cancels, and is able to get a stack trace from the processing thread at time of cancellation, then any requests toget()that result in aCancellationException, the exception will have a cause ofCancelDebuggingListenableFuture.FutureProcessingStackwith the previous stack trace included.- Since:
- 5.28
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCancelDebuggingListenableFuture.FutureProcessingStackThrowable that is not thrown, but instead added as a cause to indicate the processing stack trace at the time of cancellation.-
Nested classes/interfaces inherited from interface org.threadly.concurrent.future.ListenableFuture
ListenableFuture.ListenerOptimizationStrategy
-
-
Constructor Summary
Constructors Constructor Description CancelDebuggingListenableFuture(ListenableFuture<T> delegateFuture)Construct a newCancelDebuggingListenableFutureby wrapping the provided future.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancancel(boolean interrupt)Tget()Tget(long arg0, java.util.concurrent.TimeUnit arg1)java.lang.ThrowablegetFailure()Similar toFuture.get()except instead of providing a result, this will provide a thrown exception ifListenableFuture.isCompletedExceptionally()returnstrue.java.lang.ThrowablegetFailure(long timeout, java.util.concurrent.TimeUnit unit)Similar toFuture.get(long, TimeUnit)except instead of providing a result, this will provide a thrown exception ifListenableFuture.isCompletedExceptionally()returnstrue.java.lang.StackTraceElement[]getRunningStackTrace()A best effort to return the stack trace for for the executing thread of either this future, or a future which this depends on through the use ofListenableFuture.map(Function)or similar functions.booleanisCancelled()booleanisCompletedExceptionally()Returnstrueif the future is both done and has completed with an error or was canceled.booleanisDone()ListenableFuture<T>listener(java.lang.Runnable listener, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)Add a listener to be called once the future has completed.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.threadly.concurrent.future.ListenableFuture
callback, callback, callback, failureCallback, failureCallback, failureCallback, flatMap, flatMap, flatMap, flatMap, flatMapFailure, flatMapFailure, flatMapFailure, listener, listener, map, map, map, mapFailure, mapFailure, mapFailure, resultCallback, resultCallback, resultCallback, throwMap, throwMap, throwMap
-
-
-
-
Constructor Detail
-
CancelDebuggingListenableFuture
public CancelDebuggingListenableFuture(ListenableFuture<T> delegateFuture)
Construct a newCancelDebuggingListenableFutureby wrapping the provided future.- Parameters:
delegateFuture- A non-null future to wrap
-
-
Method Detail
-
cancel
public boolean cancel(boolean interrupt)
- Specified by:
cancelin interfacejava.util.concurrent.Future<T>
-
get
public T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
- Specified by:
getin interfacejava.util.concurrent.Future<T>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionException
-
get
public T get(long arg0, java.util.concurrent.TimeUnit arg1) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
- Specified by:
getin interfacejava.util.concurrent.Future<T>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException
-
getFailure
public java.lang.Throwable getFailure() throws java.lang.InterruptedExceptionDescription copied from interface:ListenableFutureSimilar toFuture.get()except instead of providing a result, this will provide a thrown exception ifListenableFuture.isCompletedExceptionally()returnstrue. If the future has not completed yet this function will block until completion. If the future completed normally, this will returnnull.- Specified by:
getFailurein interfaceListenableFuture<T>- Returns:
- Throwable thrown in computing the future or
nullif completed normally - Throws:
java.lang.InterruptedException- If the current thread was interrupted while blocking
-
getFailure
public java.lang.Throwable getFailure(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutExceptionDescription copied from interface:ListenableFutureSimilar toFuture.get(long, TimeUnit)except instead of providing a result, this will provide a thrown exception ifListenableFuture.isCompletedExceptionally()returnstrue. If the future has not completed yet this function will block until completion. If the future completed normally, this will returnnull.- Specified by:
getFailurein interfaceListenableFuture<T>- Parameters:
timeout- The maximum time to waitunit- The time unit of the timeout argument- Returns:
- Throwable thrown in computing the future or
nullif completed normally - Throws:
java.lang.InterruptedException- If the current thread was interrupted while blockingjava.util.concurrent.TimeoutException- If the timeout was reached before the future completed
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfacejava.util.concurrent.Future<T>
-
isDone
public boolean isDone()
- Specified by:
isDonein interfacejava.util.concurrent.Future<T>
-
isCompletedExceptionally
public boolean isCompletedExceptionally()
Description copied from interface:ListenableFutureReturnstrueif the future is both done and has completed with an error or was canceled. If this returnstruetheThrowableresponsible for the error can be retrieved usingListenableFuture.getFailure();- Specified by:
isCompletedExceptionallyin interfaceListenableFuture<T>- Returns:
trueif this ListenableFuture completed by a thrown Exception or was canceled
-
listener
public ListenableFuture<T> listener(java.lang.Runnable listener, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Description copied from interface:ListenableFutureAdd a listener to be called once the future has completed. If the future has already finished, this will be called immediately.If the provided
Executoris null, the listener will execute on the thread which computed the original future (once it is done). If the future has already completed, the listener will execute immediately on the thread which is adding the listener.Caution should be used when choosing to optimize the listener execution. If the listener is complex, or wanting to be run concurrent, this optimization could prevent that. In addition it will prevent other listeners from potentially being invoked until it completes. However if the listener is small / fast, this can provide significant performance gains. It should also be known that not all
ListenableFutureimplementations may be able to do such an optimization. Please seeListenableFuture.ListenerOptimizationStrategyjavadocs for more specific details of what optimizations are available.- Specified by:
listenerin interfaceListenableFuture<T>- Parameters:
listener- the listener to run when the computation is completeexecutor-Executorthe listener should be ran on, ornulloptimizeExecution-trueto avoid listener queuing for execution if already on the desired pool- Returns:
- Exactly
thisinstance to add more listeners or other functional operations
-
getRunningStackTrace
public java.lang.StackTraceElement[] getRunningStackTrace()
Description copied from interface:ListenableFutureA best effort to return the stack trace for for the executing thread of either this future, or a future which this depends on through the use ofListenableFuture.map(Function)or similar functions. If there is no thread executing the future yet, or the future has already completed, then this will returnnull.This is done without locking (though generating a stack trace still requires a JVM safe point), so the resulting stack trace is NOT guaranteed to be accurate. In most cases (particularly when blocking) this should be accurate though.
- Specified by:
getRunningStackTracein interfaceListenableFuture<T>- Returns:
- The stack trace currently executing the future, or
nullif unavailable
-
-