Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2012, Inc. or its affiliates. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License"). You may not
   * use this file except in compliance with the License. A copy of the License is
   * located at
  * or in the "license" file accompanying this file. This file is distributed on
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.

Promise is a future like object that is used as a placeholder for a result of an asynchronous API. Java Future is a synchronization construct that is used to block a thread that called get() method if result is not available yet. Promise differs from it as it cannot be used for blocking. A call to its get() method throws IllegalStateException if result is not available yet. The correct way to ensure that Promise is ready is to access it from a method that is annotated as

<V> The result type returned by this Promise's get method. Use java.lang.Void to represent Promise that indicates completion of operation that doesn't return a value.
and have the given Promise as one its arguments or from Task.doExecute() method assuming that promise was passed to the Task as a constructor parameter.

Promise is not linked to error handling like Future is. In case of exceptions they are propagated to the TryCatchFinally.doCatch(java.lang.Throwable) method of the TryCatchFinally that owns the asynchronous task that failed. See TryCatchFinally for more info on the error handling.

For promises that don't need a value and just used to ensure correct ordering of asynchronous operations the common pattern to use java.lang.Void as a generic type.

 public abstract class Promise<V> {


result of your asynchronous computation
java.lang.IllegalStateException if result of your asynchronous computation is not available yet
     public abstract V get();


true if the result of your asynchronous computation is available
     public abstract boolean isReady();


human friendly description on what this promise represents. Emitted for example as part of AsyncScope.getAsynchronousThreadDumpAsString()
     public String getDescription() {
         return null;

Compliant implementation should notify callbacks after promise is set to ready state.

callback callback to notify
     protected abstract void addCallback(Runnable callback);
     protected abstract void removeCallback(Runnable callback);

Convenience method for creating a Promise object which is in ready state and returns the passed in value when get() is called.

The same as new Settable<T>(value)

<T> Type of value
value Object to return when get() is called
An instance of Promise wrapper object
     public static <T> Promise<T> asPromise(T value) {
         return new Settable<T>(value);

This is a factory method to create a Promise<Void> object in ready state.

An instance of Promise object with no value.
    public static Promise<VoidVoid() {
        return new Settable<Void>(null);
New to GrepCode? Check out our FAQ X