1. /*
  2. * @(#)Future.java 1.6 04/02/09
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent;
  8. /**
  9. * A <tt>Future</tt> represents the result of an asynchronous
  10. * computation. Methods are provided to check if the computation is
  11. * complete, to wait for its completion, and to retrieve the result of
  12. * the computation. The result can only be retrieved using method
  13. * <tt>get</tt> when the computation has completed, blocking if
  14. * necessary until it is ready. Cancellation is performed by the
  15. * <tt>cancel</tt> method. Additional methods are provided to
  16. * determine if the task completed normally or was cancelled. Once a
  17. * computation has completed, the computation cannot be cancelled.
  18. * If you would like to use a <tt>Future</tt> for the sake
  19. * of cancellability but not provide a usable result, you can
  20. * declare types of the form <tt>Future<?></tt> and
  21. * return <tt>null</tt> as a result of the underlying task.
  22. *
  23. * <p>
  24. * <b>Sample Usage</b> (Note that the following classes are all
  25. * made-up.) <p>
  26. * <pre>
  27. * interface ArchiveSearcher { String search(String target); }
  28. * class App {
  29. * ExecutorService executor = ...
  30. * ArchiveSearcher searcher = ...
  31. * void showSearch(final String target) throws InterruptedException {
  32. * Future<String> future = executor.submit(new Callable<String>() {
  33. * public String call() { return searcher.search(target); }
  34. * });
  35. * displayOtherThings(); // do other things while searching
  36. * try {
  37. * displayText(future.get()); // use future
  38. * } catch (ExecutionException ex) { cleanup(); return; }
  39. * }
  40. * }
  41. * </pre>
  42. *
  43. * The {@link FutureTask} class is an implementation of <tt>Future</tt> that
  44. * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>.
  45. * For example, the above construction with <tt>submit</tt> could be replaced by:
  46. * <pre>
  47. * FutureTask<String> future =
  48. * new FutureTask<String>(new Callable<String>() {
  49. * public String call() {
  50. * return searcher.search(target);
  51. * }});
  52. * executor.execute(future);
  53. * </pre>
  54. * @see FutureTask
  55. * @see Executor
  56. * @since 1.5
  57. * @author Doug Lea
  58. * @param <V> The result type returned by this Future's <tt>get</tt> method
  59. */
  60. public interface Future<V> {
  61. /**
  62. * Attempts to cancel execution of this task. This attempt will
  63. * fail if the task has already completed, already been cancelled,
  64. * or could not be cancelled for some other reason. If successful,
  65. * and this task has not started when <tt>cancel</tt> is called,
  66. * this task should never run. If the task has already started,
  67. * then the <tt>mayInterruptIfRunning</tt> parameter determines
  68. * whether the thread executing this task should be interrupted in
  69. * an attempt to stop the task.
  70. *
  71. * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
  72. * task should be interrupted; otherwise, in-progress tasks are allowed
  73. * to complete
  74. * @return <tt>false</tt> if the task could not be cancelled,
  75. * typically because it has already completed normally;
  76. * <tt>true</tt> otherwise
  77. */
  78. boolean cancel(boolean mayInterruptIfRunning);
  79. /**
  80. * Returns <tt>true</tt> if this task was cancelled before it completed
  81. * normally.
  82. *
  83. * @return <tt>true</tt> if task was cancelled before it completed
  84. */
  85. boolean isCancelled();
  86. /**
  87. * Returns <tt>true</tt> if this task completed.
  88. *
  89. * Completion may be due to normal termination, an exception, or
  90. * cancellation -- in all of these cases, this method will return
  91. * <tt>true</tt>.
  92. *
  93. * @return <tt>true</tt> if this task completed.
  94. */
  95. boolean isDone();
  96. /**
  97. * Waits if necessary for the computation to complete, and then
  98. * retrieves its result.
  99. *
  100. * @return the computed result
  101. * @throws CancellationException if the computation was cancelled
  102. * @throws ExecutionException if the computation threw an
  103. * exception
  104. * @throws InterruptedException if the current thread was interrupted
  105. * while waiting
  106. */
  107. V get() throws InterruptedException, ExecutionException;
  108. /**
  109. * Waits if necessary for at most the given time for the computation
  110. * to complete, and then retrieves its result, if available.
  111. *
  112. * @param timeout the maximum time to wait
  113. * @param unit the time unit of the timeout argument
  114. * @return the computed result
  115. * @throws CancellationException if the computation was cancelled
  116. * @throws ExecutionException if the computation threw an
  117. * exception
  118. * @throws InterruptedException if the current thread was interrupted
  119. * while waiting
  120. * @throws TimeoutException if the wait timed out
  121. */
  122. V get(long timeout, TimeUnit unit)
  123. throws InterruptedException, ExecutionException, TimeoutException;
  124. }