< prev index next >

src/java.base/share/classes/java/util/concurrent/Future.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2020-12
Reviewed-by: martin


  39  * A {@code Future} represents the result of an asynchronous
  40  * computation.  Methods are provided to check if the computation is
  41  * complete, to wait for its completion, and to retrieve the result of
  42  * the computation.  The result can only be retrieved using method
  43  * {@code get} when the computation has completed, blocking if
  44  * necessary until it is ready.  Cancellation is performed by the
  45  * {@code cancel} method.  Additional methods are provided to
  46  * determine if the task completed normally or was cancelled. Once a
  47  * computation has completed, the computation cannot be cancelled.
  48  * If you would like to use a {@code Future} for the sake
  49  * of cancellability but not provide a usable result, you can
  50  * declare types of the form {@code Future<?>} and
  51  * return {@code null} as a result of the underlying task.
  52  *
  53  * <p><b>Sample Usage</b> (Note that the following classes are all
  54  * made-up.)
  55  *
  56  * <pre> {@code
  57  * interface ArchiveSearcher { String search(String target); }
  58  * class App {
  59  *   ExecutorService executor = ...
  60  *   ArchiveSearcher searcher = ...
  61  *   void showSearch(String target) throws InterruptedException {
  62  *     Callable<String> task = () -> searcher.search(target);
  63  *     Future<String> future = executor.submit(task);
  64  *     displayOtherThings(); // do other things while searching
  65  *     try {
  66  *       displayText(future.get()); // use future
  67  *     } catch (ExecutionException ex) { cleanup(); return; }
  68  *   }
  69  * }}</pre>
  70  *
  71  * The {@link FutureTask} class is an implementation of {@code Future} that
  72  * implements {@code Runnable}, and so may be executed by an {@code Executor}.
  73  * For example, the above construction with {@code submit} could be replaced by:
  74  * <pre> {@code
  75  * FutureTask<String> future = new FutureTask<>(task);
  76  * executor.execute(future);}</pre>
  77  *
  78  * <p>Memory consistency effects: Actions taken by the asynchronous computation
  79  * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
  80  * actions following the corresponding {@code Future.get()} in another thread.
  81  *
  82  * @see FutureTask
  83  * @see Executor
  84  * @since 1.5
  85  * @author Doug Lea
  86  * @param <V> The result type returned by this Future's {@code get} method
  87  */
  88 public interface Future<V> {
  89 
  90     /**
  91      * Attempts to cancel execution of this task.  This attempt will
  92      * fail if the task has already completed, has already been cancelled,
  93      * or could not be cancelled for some other reason. If successful,
  94      * and this task has not started when {@code cancel} is called,
  95      * this task should never run.  If the task has already started,
  96      * then the {@code mayInterruptIfRunning} parameter determines
  97      * whether the thread executing this task should be interrupted in
  98      * an attempt to stop the task.
  99      *
 100      * <p>After this method returns, subsequent calls to {@link #isDone} will
 101      * always return {@code true}.  Subsequent calls to {@link #isCancelled}
 102      * will always return {@code true} if this method returned {@code true}.
 103      *
 104      * @param mayInterruptIfRunning {@code true} if the thread executing this
 105      * task should be interrupted; otherwise, in-progress tasks are allowed
 106      * to complete

 107      * @return {@code false} if the task could not be cancelled,
 108      * typically because it has already completed normally;
 109      * {@code true} otherwise


 110      */
 111     boolean cancel(boolean mayInterruptIfRunning);
 112 
 113     /**
 114      * Returns {@code true} if this task was cancelled before it completed
 115      * normally.
 116      *
 117      * @return {@code true} if this task was cancelled before it completed
 118      */
 119     boolean isCancelled();
 120 
 121     /**
 122      * Returns {@code true} if this task completed.
 123      *
 124      * Completion may be due to normal termination, an exception, or
 125      * cancellation -- in all of these cases, this method will return
 126      * {@code true}.
 127      *
 128      * @return {@code true} if this task completed
 129      */




  39  * A {@code Future} represents the result of an asynchronous
  40  * computation.  Methods are provided to check if the computation is
  41  * complete, to wait for its completion, and to retrieve the result of
  42  * the computation.  The result can only be retrieved using method
  43  * {@code get} when the computation has completed, blocking if
  44  * necessary until it is ready.  Cancellation is performed by the
  45  * {@code cancel} method.  Additional methods are provided to
  46  * determine if the task completed normally or was cancelled. Once a
  47  * computation has completed, the computation cannot be cancelled.
  48  * If you would like to use a {@code Future} for the sake
  49  * of cancellability but not provide a usable result, you can
  50  * declare types of the form {@code Future<?>} and
  51  * return {@code null} as a result of the underlying task.
  52  *
  53  * <p><b>Sample Usage</b> (Note that the following classes are all
  54  * made-up.)
  55  *
  56  * <pre> {@code
  57  * interface ArchiveSearcher { String search(String target); }
  58  * class App {
  59  *   ExecutorService executor = ...;
  60  *   ArchiveSearcher searcher = ...;
  61  *   void showSearch(String target) throws InterruptedException {
  62  *     Callable<String> task = () -> searcher.search(target);
  63  *     Future<String> future = executor.submit(task);
  64  *     displayOtherThings(); // do other things while searching
  65  *     try {
  66  *       displayText(future.get()); // use future
  67  *     } catch (ExecutionException ex) { cleanup(); return; }
  68  *   }
  69  * }}</pre>
  70  *
  71  * The {@link FutureTask} class is an implementation of {@code Future} that
  72  * implements {@code Runnable}, and so may be executed by an {@code Executor}.
  73  * For example, the above construction with {@code submit} could be replaced by:
  74  * <pre> {@code
  75  * FutureTask<String> future = new FutureTask<>(task);
  76  * executor.execute(future);}</pre>
  77  *
  78  * <p>Memory consistency effects: Actions taken by the asynchronous computation
  79  * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
  80  * actions following the corresponding {@code Future.get()} in another thread.
  81  *
  82  * @see FutureTask
  83  * @see Executor
  84  * @since 1.5
  85  * @author Doug Lea
  86  * @param <V> The result type returned by this Future's {@code get} method
  87  */
  88 public interface Future<V> {
  89 
  90     /**
  91      * Attempts to cancel execution of this task.  This method has no
  92      * effect if the task is already completed or cancelled, or could
  93      * not be cancelled for some other reason.  Otherwise, if this
  94      * task has not started when {@code cancel} is called, this task
  95      * should never run.  If the task has already started, then the
  96      * {@code mayInterruptIfRunning} parameter determines whether the
  97      * thread executing this task (when known by the implementation)
  98      * is interrupted in an attempt to stop the task.
  99      *
 100      * <p>The return value from this method does not necessarily
 101      * indicate whether the task is now cancelled; use {@link
 102      * #isCancelled}.
 103      *
 104      * @param mayInterruptIfRunning {@code true} if the thread
 105      * executing this task should be interrupted (if the thread is
 106      * known to the implementation); otherwise, in-progress tasks are
 107      * allowed to complete
 108      * @return {@code false} if the task could not be cancelled,
 109      * typically because it has already completed; {@code true}
 110      * otherwise. If two or more threads cause a task to be cancelled,
 111      * then at least one of them returns {@code true}. Implementations
 112      * may provide stronger guarantees.
 113      */
 114     boolean cancel(boolean mayInterruptIfRunning);
 115 
 116     /**
 117      * Returns {@code true} if this task was cancelled before it completed
 118      * normally.
 119      *
 120      * @return {@code true} if this task was cancelled before it completed
 121      */
 122     boolean isCancelled();
 123 
 124     /**
 125      * Returns {@code true} if this task completed.
 126      *
 127      * Completion may be due to normal termination, an exception, or
 128      * cancellation -- in all of these cases, this method will return
 129      * {@code true}.
 130      *
 131      * @return {@code true} if this task completed
 132      */


< prev index next >