< prev index next >

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

Print this page
@@ -436,11 +436,11 @@
       */
      private static final Executor ASYNC_POOL = USE_COMMON_POOL ?
          ForkJoinPool.commonPool() : new ThreadPerTaskExecutor();
  
      /** Fallback if ForkJoinPool.commonPool() cannot support parallelism */
-     static final class ThreadPerTaskExecutor implements Executor {
+     private static final class ThreadPerTaskExecutor implements Executor {
          public void execute(Runnable r) {
              Objects.requireNonNull(r);
              new Thread(r).start();
          }
      }

@@ -2132,10 +2132,34 @@
      public T getNow(T valueIfAbsent) {
          Object r;
          return ((r = result) == null) ? valueIfAbsent : (T) reportJoin(r);
      }
  
+     @Override
+     public T completedResultNow() {
+         Object r = result;
+         if (r != null) {
+             if (r instanceof AltResult alt) {
+                 if (alt.ex == null) return null;
+             } else {
+                 @SuppressWarnings("unchecked")
+                 T t = (T) r;
+                 return t;
+             }
+         }
+         throw new IllegalStateException();
+     }
+ 
+     @Override
+     public Throwable completedExceptionNow() {
+         Object r = result;
+         if (r instanceof AltResult alt && alt.ex != null) {
+             return alt.ex;
+         }
+         throw new IllegalStateException();
+     }
+ 
      /**
       * If not already completed, sets the value returned by {@link
       * #get()} and related methods to the given value.
       *
       * @param value the result value

@@ -2507,10 +2531,24 @@
      public boolean isCompletedExceptionally() {
          Object r;
          return ((r = result) instanceof AltResult) && r != NIL;
      }
  
+     @Override
+     public State state() {
+         Object r = result;
+         if (r == null)
+             return State.RUNNING;
+         if (r != NIL && r instanceof AltResult alt) {
+             if (alt.ex instanceof CancellationException)
+                 return State.CANCELLED;
+             else
+                 return State.FAILED;
+         }
+         return State.SUCCESS;
+     }
+ 
      /**
       * Forcibly sets or resets the value subsequently returned by
       * method {@link #get()} and related methods, whether or not
       * already completed. This method is designed for use only in
       * error recovery actions, and even in such situations may result

@@ -2910,10 +2948,14 @@
              throw new UnsupportedOperationException(); }
          @Override public T getNow(T valueIfAbsent) {
              throw new UnsupportedOperationException(); }
          @Override public T join() {
              throw new UnsupportedOperationException(); }
+         @Override public T completedResultNow() {
+             throw new UnsupportedOperationException(); }
+         @Override public Throwable completedExceptionNow() {
+             throw new UnsupportedOperationException(); }
          @Override public boolean complete(T value) {
              throw new UnsupportedOperationException(); }
          @Override public boolean completeExceptionally(Throwable ex) {
              throw new UnsupportedOperationException(); }
          @Override public boolean cancel(boolean mayInterruptIfRunning) {

@@ -2926,10 +2968,12 @@
              throw new UnsupportedOperationException(); }
          @Override public boolean isCancelled() {
              throw new UnsupportedOperationException(); }
          @Override public boolean isCompletedExceptionally() {
              throw new UnsupportedOperationException(); }
+         @Override public State state() {
+             throw new UnsupportedOperationException(); }
          @Override public int getNumberOfDependents() {
              throw new UnsupportedOperationException(); }
          @Override public CompletableFuture<T> completeAsync
              (Supplier<? extends T> supplier, Executor executor) {
              throw new UnsupportedOperationException(); }
< prev index next >