com.atlassian.util.concurrent
Class SettableFuture<T>

java.lang.Object
  extended by com.atlassian.util.concurrent.SettableFuture<T>
All Implemented Interfaces:
java.util.concurrent.Future<T>

@ThreadSafe
public class SettableFuture<T>
extends java.lang.Object
implements java.util.concurrent.Future<T>

SettableFuture is a Future implementation where the responsibility for producing the result is external to the future instance, unlike FutureTask where the future holds the operation (a Callable or Runnable instance) and the first thread that calls FutureTask.run() executes the operation.

This is useful in situations where all the inputs may not be available at construction time.


Constructor Summary
SettableFuture()
           
 
Method Summary
 boolean cancel(boolean mayInterruptIfRunning)
           
 T get()
           
 T get(long timeout, java.util.concurrent.TimeUnit unit)
           
 boolean isCancelled()
           
 boolean isDone()
           
 SettableFuture<T> set(T value)
          Set the value returned by get() and get(long, TimeUnit)
 SettableFuture<T> setException(java.lang.Throwable throwable)
          Set the exception thrown as the causal exception of an ExecutionException by get() and get(long, TimeUnit)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SettableFuture

public SettableFuture()
Method Detail

set

public SettableFuture<T> set(T value)
Set the value returned by get() and get(long, TimeUnit)

Note that this can only be done once unless the value of the second set equals the first value otherwise an exception will be thrown. It also cannot be set if this future has been cancelled or an exception has been set.

Parameters:
value - the value to be set.

setException

public SettableFuture<T> setException(java.lang.Throwable throwable)
Set the exception thrown as the causal exception of an ExecutionException by get() and get(long, TimeUnit)

Note that this can only be done once unless the value of the second setException(Throwable) equals the first value otherwise an exception will be thrown (as most exceptions do not implement equals this effectively means the same reference). It also cannot be set if this future has been cancelled or a a value has been set.

Parameters:
value - the value to be set.

get

public T get()
      throws java.lang.InterruptedException,
             java.util.concurrent.ExecutionException
Specified by:
get in interface java.util.concurrent.Future<T>
Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException

get

public T get(long timeout,
             java.util.concurrent.TimeUnit unit)
      throws java.lang.InterruptedException,
             java.util.concurrent.ExecutionException,
             java.util.concurrent.TimeoutException
Specified by:
get in interface java.util.concurrent.Future<T>
Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException

isDone

public boolean isDone()
Specified by:
isDone in interface java.util.concurrent.Future<T>

isCancelled

public boolean isCancelled()
Specified by:
isCancelled in interface java.util.concurrent.Future<T>

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Specified by:
cancel in interface java.util.concurrent.Future<T>


Copyright © 2011 Atlassian. All Rights Reserved.