Class SettableListenableFuture<T>
- java.lang.Object
- org.springframework.util.concurrent.SettableListenableFuture<T>
- All Implemented Interfaces:
Future<T>
,ListenableFuture<T>
public class SettableListenableFuture<T> extends Object implements ListenableFuture<T>
AListenableFuture
whose value can be set viaset(Object)
orsetException(Throwable)
. It may also get cancelled.Inspired by
com.google.common.util.concurrent.SettableFuture
.- Since:
- 4.1
- Author:
- Mattias Severson, Rossen Stoyanchev, Juergen Hoeller
Constructor Summary
Constructors Constructor Description SettableListenableFuture()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCallback(ListenableFutureCallback<? super T> callback)
Register the givenListenableFutureCallback
.void
addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback)
Java 8 lambda-friendly alternative with success and failure callbacks.boolean
cancel(boolean mayInterruptIfRunning)
T
get()
Retrieve the value.T
get(long timeout, TimeUnit unit)
Retrieve the value.protected void
interruptTask()
Subclasses can override this method to implement interruption of the future's computation.boolean
isCancelled()
boolean
isDone()
boolean
set(T value)
Set the value of this future.boolean
setException(Throwable exception)
Set the exception of this future.
Constructor Detail
SettableListenableFuture
public SettableListenableFuture()
Method Detail
set
public boolean set(T value)
Set the value of this future. This method will returntrue
if the value was set successfully, orfalse
if the future has already been set or cancelled.- Parameters:
value
- the value that will be set- Returns:
true
if the value was successfully set, elsefalse
setException
public boolean setException(Throwable exception)
Set the exception of this future. This method will returntrue
if the exception was set successfully, orfalse
if the future has already been set or cancelled.- Parameters:
exception
- the value that will be set- Returns:
true
if the exception was successfully set, elsefalse
addCallback
public void addCallback(ListenableFutureCallback<? super T> callback)
Description copied from interface:ListenableFuture
Register the givenListenableFutureCallback
.- Specified by:
addCallback
in interfaceListenableFuture<T>
- Parameters:
callback
- the callback to register
addCallback
public void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback)
Description copied from interface:ListenableFuture
Java 8 lambda-friendly alternative with success and failure callbacks.- Specified by:
addCallback
in interfaceListenableFuture<T>
- Parameters:
successCallback
- the success callbackfailureCallback
- the failure callback
cancel
public boolean cancel(boolean mayInterruptIfRunning)
isCancelled
public boolean isCancelled()
- Specified by:
isCancelled
in interfaceFuture<T>
get
public T get() throws InterruptedException, ExecutionException
Retrieve the value.This method returns the value if it has been set via
set(Object)
, throws anExecutionException
if an exception has been set viasetException(Throwable)
, or throws aCancellationException
if the future has been cancelled.- Specified by:
get
in interfaceFuture<T>
- Returns:
- the value associated with this future
- Throws:
InterruptedException
ExecutionException
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Retrieve the value.This method returns the value if it has been set via
set(Object)
, throws anExecutionException
if an exception has been set viasetException(Throwable)
, or throws aCancellationException
if the future has been cancelled.- Specified by:
get
in interfaceFuture<T>
- Parameters:
timeout
- the maximum time to waitunit
- the unit of the timeout argument- Returns:
- the value associated with this future
- Throws:
InterruptedException
ExecutionException
TimeoutException
interruptTask
protected void interruptTask()
Subclasses can override this method to implement interruption of the future's computation. The method is invoked automatically by a successful call tocancel(true)
.The default implementation is empty.