< prev index next >

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

Print this page
*** 159,6 ***
--- 159,130 ---
       * while waiting
       * @throws TimeoutException if the wait timed out
       */
      V get(long timeout, TimeUnit unit)
          throws InterruptedException, ExecutionException, TimeoutException;
+ 
+     /**
+      * Returns the computed result, without waiting.
+      *
+      * @implSpec
+      * The default implementation invokes {@code isDone()} to test if the task
+      * has completed. If done, it invokes {@code get()} to obtain the result.
+      *
+      * @return the computed result
+      * @throws IllegalStateException if the task has not completed or the task
+      * did not complete with a result
+      * @since 99
+      */
+     default V completedResultNow() {
+         if (!isDone())
+             throw new IllegalStateException();
+         boolean interrupted = false;
+         try {
+             while (true) {
+                 try {
+                     return get();
+                 } catch (InterruptedException e) {
+                     interrupted = true;
+                 } catch (ExecutionException | CancellationException e) {
+                     throw new IllegalStateException();
+                 }
+             }
+         } finally {
+             if (interrupted) Thread.currentThread().interrupt();
+         }
+     }
+ 
+     /**
+      * Returns the exception thrown by the task or a CancellationException
+      * if cancelled, without waiting.
+      *
+      * @implSpec
+      * The default implementation invokes {@code isDone()} to test if the task
+      * has completed. If done and not cancelled, it invokes {@code get()} and
+      * catches the {@code ExecutionException} to obtain the exception.
+      *
+      * @return the exception thrown by the task
+      * @throws IllegalStateException if the task has not completed or the task
+      * completed normally
+      * @since 99
+      */
+     default Throwable completedExceptionNow() {
+         if (!isDone())
+             throw new IllegalStateException();
+         if (isCancelled())
+             return new CancellationException();
+         boolean interrupted = false;
+         try {
+             while (true) {
+                 try {
+                     get();
+                     throw new IllegalStateException();
+                 } catch (InterruptedException e) {
+                     interrupted = true;
+                 } catch (ExecutionException e) {
+                     return e.getCause();
+                 }
+             }
+         } finally {
+             if (interrupted) Thread.currentThread().interrupt();
+         }
+     }
+ 
+     /**
+      * Represents the computation state.
+      * @since 99
+      */
+     enum State {
+         /**
+          * The task has not completed.
+          */
+         RUNNING,
+         /**
+          * The task completed with a result.
+          * @see Future#completedResultNow()
+          */
+         SUCCESS,
+         /**
+          * The task completed with an exception.
+          * @see Future#completedExceptionNow()
+          */
+         FAILED,
+         /**
+          * The task is cancelled.
+          * @see #cancel(boolean)
+          */
+         CANCELLED
+     }
+ 
+     /**
+      * {@return the computation state}
+      *
+      * @implSpec
+      * The default implementation uses {@code isDone()}, {@code isCancelled()},
+      * and {@code get()} to determine the state.
+      *
+      * @since 99
+      */
+     default State state() {
+         if (!isDone())
+             return State.RUNNING;
+         if (isCancelled())
+             return State.CANCELLED;
+         boolean interrupted = false;
+         try {
+             while (true) {
+                 try {
+                     get();  // may throw InterruptedException when done
+                     return State.SUCCESS;
+                 } catch (InterruptedException e) {
+                     interrupted = true;
+                 } catch (ExecutionException e) {
+                     return State.FAILED;
+                 }
+             }
+         } finally {
+             if (interrupted) Thread.currentThread().interrupt();
+         }
+     }
  }
< prev index next >