< prev index next >

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

Print this page

 390      * @param interruptible true if park interruptibly when external
 391      * @param timed true if use timed wait
 392      * @param nanos if timed, timeout value
 393      * @return ABNORMAL if interrupted, else status on exit
 394      */
 395     private int awaitDone(ForkJoinPool pool, boolean ran,
 396                           boolean interruptible, boolean timed,
 397                           long nanos) {
 398         ForkJoinPool p; boolean internal; int s; Thread t;
 399         ForkJoinPool.WorkQueue q = null;
 400         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
 401             ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
 402             p = wt.pool;
 403             if (pool == null)
 404                 pool = p;
 405             if (internal = (pool == p))
 406                 q = wt.workQueue;
 407         }
 408         else {
 409             internal = false;
 410             p = ForkJoinPool.common;
 411             if (pool == null)
 412                 pool = p;
 413             if (pool == p && p != null)
 414                 q = p.externalQueue();
 415         }
 416         if (interruptible && Thread.interrupted())
 417             return ABNORMAL;
 418         if ((s = status) < 0)
 419             return s;
 420         long deadline = 0L;
 421         if (timed) {
 422             if (nanos <= 0L)
 423                 return 0;
 424             else if ((deadline = nanos + System.nanoTime()) == 0L)
 425                 deadline = 1L;
 426         }
 427         boolean uncompensate = false;
 428         if (q != null && p != null) {  // try helping
 429             // help even in timed mode if pool has no parallelism
 430             boolean canHelp = !timed || (p.mode & SMASK) == 0;

 632     /**
 633      * Arranges to asynchronously execute this task in the pool the
 634      * current task is running in, if applicable, or using the {@link
 635      * ForkJoinPool#commonPool()} if not {@link #inForkJoinPool}.  While
 636      * it is not necessarily enforced, it is a usage error to fork a
 637      * task more than once unless it has completed and been
 638      * reinitialized.  Subsequent modifications to the state of this
 639      * task or any data it operates on are not necessarily
 640      * consistently observable by any thread other than the one
 641      * executing it unless preceded by a call to {@link #join} or
 642      * related methods, or a call to {@link #isDone} returning {@code
 643      * true}.
 644      *
 645      * @return {@code this}, to simplify usage
 646      */
 647     public final ForkJoinTask<V> fork() {
 648         Thread t; ForkJoinWorkerThread w;
 649         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
 650             (w = (ForkJoinWorkerThread)t).workQueue.push(this, w.pool);
 651         else
 652             ForkJoinPool.common.externalPush(this);
 653         return this;
 654     }
 655 
 656     /**
 657      * Returns the result of the computation when it
 658      * {@linkplain #isDone is done}.
 659      * This method differs from {@link #get()} in that abnormal
 660      * completion results in {@code RuntimeException} or {@code Error},
 661      * not {@code ExecutionException}, and that interrupts of the
 662      * calling thread do <em>not</em> cause the method to abruptly
 663      * return by throwing {@code InterruptedException}.
 664      *
 665      * @return the computed result
 666      */
 667     public final V join() {
 668         int s;
 669         if ((s = status) >= 0)
 670             s = awaitDone(null, false, false, false, 0L);
 671         if ((s & ABNORMAL) != 0)
 672             reportException(s);

 881     /**
 882      * Returns {@code true} if this task threw an exception or was cancelled.
 883      *
 884      * @return {@code true} if this task threw an exception or was cancelled
 885      */
 886     public final boolean isCompletedAbnormally() {
 887         return (status & ABNORMAL) != 0;
 888     }
 889 
 890     /**
 891      * Returns {@code true} if this task completed without throwing an
 892      * exception and was not cancelled.
 893      *
 894      * @return {@code true} if this task completed without throwing an
 895      * exception and was not cancelled
 896      */
 897     public final boolean isCompletedNormally() {
 898         return (status & (DONE | ABNORMAL)) == DONE;
 899     }
 900 


































 901     /**
 902      * Returns the exception thrown by the base computation, or a
 903      * {@code CancellationException} if cancelled, or {@code null} if
 904      * none or if the method has not yet completed.
 905      *
 906      * @return the exception, or {@code null} if none
 907      */
 908     public final Throwable getException() {
 909         return getException(status);
 910     }
 911 
 912     /**
 913      * Completes this task abnormally, and if not already aborted or
 914      * cancelled, causes it to throw the given exception upon
 915      * {@code join} and related operations. This method may be used
 916      * to induce exceptions in asynchronous tasks, or to force
 917      * completion of tasks that would not otherwise complete.  Its use
 918      * in other situations is discouraged.  This method is
 919      * overridable, but overridden versions must invoke {@code super}
 920      * implementation to maintain guarantees.

1053         throws InterruptedException, ExecutionException, TimeoutException {
1054         int s = awaitDone(pool, false, true, true, nanos);
1055         if (s >= 0 || (s & ABNORMAL) != 0)
1056             reportExecutionException(s);
1057         return getRawResult();
1058     }
1059 
1060     /**
1061      * Possibly executes tasks until the pool hosting the current task
1062      * {@linkplain ForkJoinPool#isQuiescent is quiescent}.  This
1063      * method may be of use in designs in which many tasks are forked,
1064      * but none are explicitly joined, instead executing them until
1065      * all are processed.
1066      */
1067     public static void helpQuiesce() {
1068         Thread t; ForkJoinWorkerThread w; ForkJoinPool p;
1069         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread &&
1070             (p = (w = (ForkJoinWorkerThread)t).pool) != null)
1071             p.helpQuiescePool(w.workQueue, Long.MAX_VALUE, false);
1072         else
1073             ForkJoinPool.common.externalHelpQuiescePool(Long.MAX_VALUE, false);
1074     }
1075 
1076     /**
1077      * Resets the internal bookkeeping state of this task, allowing a
1078      * subsequent {@code fork}. This method allows repeated reuse of
1079      * this task, but only if reuse occurs when this task has either
1080      * never been forked, or has been forked, then completed and all
1081      * outstanding joins of this task have also completed. Effects
1082      * under any other usage conditions are not guaranteed.
1083      * This method may be useful when executing
1084      * pre-constructed trees of subtasks in loops.
1085      *
1086      * <p>Upon completion of this method, {@code isDone()} reports
1087      * {@code false}, and {@code getException()} reports {@code
1088      * null}. However, the value returned by {@code getRawResult} is
1089      * unaffected. To clear this value, you can invoke {@code
1090      * setRawResult(null)}.
1091      */
1092     public void reinitialize() {
1093         aux = null;

 390      * @param interruptible true if park interruptibly when external
 391      * @param timed true if use timed wait
 392      * @param nanos if timed, timeout value
 393      * @return ABNORMAL if interrupted, else status on exit
 394      */
 395     private int awaitDone(ForkJoinPool pool, boolean ran,
 396                           boolean interruptible, boolean timed,
 397                           long nanos) {
 398         ForkJoinPool p; boolean internal; int s; Thread t;
 399         ForkJoinPool.WorkQueue q = null;
 400         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
 401             ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
 402             p = wt.pool;
 403             if (pool == null)
 404                 pool = p;
 405             if (internal = (pool == p))
 406                 q = wt.workQueue;
 407         }
 408         else {
 409             internal = false;
 410             p = ForkJoinPool.CommonPool.common;
 411             if (pool == null)
 412                 pool = p;
 413             if (pool == p && p != null)
 414                 q = p.externalQueue();
 415         }
 416         if (interruptible && Thread.interrupted())
 417             return ABNORMAL;
 418         if ((s = status) < 0)
 419             return s;
 420         long deadline = 0L;
 421         if (timed) {
 422             if (nanos <= 0L)
 423                 return 0;
 424             else if ((deadline = nanos + System.nanoTime()) == 0L)
 425                 deadline = 1L;
 426         }
 427         boolean uncompensate = false;
 428         if (q != null && p != null) {  // try helping
 429             // help even in timed mode if pool has no parallelism
 430             boolean canHelp = !timed || (p.mode & SMASK) == 0;

 632     /**
 633      * Arranges to asynchronously execute this task in the pool the
 634      * current task is running in, if applicable, or using the {@link
 635      * ForkJoinPool#commonPool()} if not {@link #inForkJoinPool}.  While
 636      * it is not necessarily enforced, it is a usage error to fork a
 637      * task more than once unless it has completed and been
 638      * reinitialized.  Subsequent modifications to the state of this
 639      * task or any data it operates on are not necessarily
 640      * consistently observable by any thread other than the one
 641      * executing it unless preceded by a call to {@link #join} or
 642      * related methods, or a call to {@link #isDone} returning {@code
 643      * true}.
 644      *
 645      * @return {@code this}, to simplify usage
 646      */
 647     public final ForkJoinTask<V> fork() {
 648         Thread t; ForkJoinWorkerThread w;
 649         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
 650             (w = (ForkJoinWorkerThread)t).workQueue.push(this, w.pool);
 651         else
 652             ForkJoinPool.CommonPool.common.externalPush(this);
 653         return this;
 654     }
 655 
 656     /**
 657      * Returns the result of the computation when it
 658      * {@linkplain #isDone is done}.
 659      * This method differs from {@link #get()} in that abnormal
 660      * completion results in {@code RuntimeException} or {@code Error},
 661      * not {@code ExecutionException}, and that interrupts of the
 662      * calling thread do <em>not</em> cause the method to abruptly
 663      * return by throwing {@code InterruptedException}.
 664      *
 665      * @return the computed result
 666      */
 667     public final V join() {
 668         int s;
 669         if ((s = status) >= 0)
 670             s = awaitDone(null, false, false, false, 0L);
 671         if ((s & ABNORMAL) != 0)
 672             reportException(s);

 881     /**
 882      * Returns {@code true} if this task threw an exception or was cancelled.
 883      *
 884      * @return {@code true} if this task threw an exception or was cancelled
 885      */
 886     public final boolean isCompletedAbnormally() {
 887         return (status & ABNORMAL) != 0;
 888     }
 889 
 890     /**
 891      * Returns {@code true} if this task completed without throwing an
 892      * exception and was not cancelled.
 893      *
 894      * @return {@code true} if this task completed without throwing an
 895      * exception and was not cancelled
 896      */
 897     public final boolean isCompletedNormally() {
 898         return (status & (DONE | ABNORMAL)) == DONE;
 899     }
 900 
 901     @Override
 902     public State state() {
 903         int s = status;
 904         if (s < 0) {
 905             if ((s & (DONE | ABNORMAL)) == DONE)
 906                 return State.SUCCESS;
 907             if ((s & (ABNORMAL | THROWN)) == (ABNORMAL | THROWN))
 908                 return State.FAILED;
 909             else
 910                 return State.CANCELLED;
 911         } else {
 912             return State.RUNNING;
 913         }
 914     }
 915 
 916     @Override
 917     public V completedResultNow() {
 918         if ((status & (DONE | ABNORMAL)) == DONE) {
 919             return getRawResult();
 920         } else {
 921             throw new IllegalStateException();
 922         }
 923     }
 924 
 925     @Override
 926     public Throwable completedExceptionNow() {
 927         Throwable ex = getException(status);
 928         if (ex != null) {
 929             return ex;
 930         } else {
 931             throw new IllegalStateException();
 932         }
 933     }
 934 
 935     /**
 936      * Returns the exception thrown by the base computation, or a
 937      * {@code CancellationException} if cancelled, or {@code null} if
 938      * none or if the method has not yet completed.
 939      *
 940      * @return the exception, or {@code null} if none
 941      */
 942     public final Throwable getException() {
 943         return getException(status);
 944     }
 945 
 946     /**
 947      * Completes this task abnormally, and if not already aborted or
 948      * cancelled, causes it to throw the given exception upon
 949      * {@code join} and related operations. This method may be used
 950      * to induce exceptions in asynchronous tasks, or to force
 951      * completion of tasks that would not otherwise complete.  Its use
 952      * in other situations is discouraged.  This method is
 953      * overridable, but overridden versions must invoke {@code super}
 954      * implementation to maintain guarantees.

1087         throws InterruptedException, ExecutionException, TimeoutException {
1088         int s = awaitDone(pool, false, true, true, nanos);
1089         if (s >= 0 || (s & ABNORMAL) != 0)
1090             reportExecutionException(s);
1091         return getRawResult();
1092     }
1093 
1094     /**
1095      * Possibly executes tasks until the pool hosting the current task
1096      * {@linkplain ForkJoinPool#isQuiescent is quiescent}.  This
1097      * method may be of use in designs in which many tasks are forked,
1098      * but none are explicitly joined, instead executing them until
1099      * all are processed.
1100      */
1101     public static void helpQuiesce() {
1102         Thread t; ForkJoinWorkerThread w; ForkJoinPool p;
1103         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread &&
1104             (p = (w = (ForkJoinWorkerThread)t).pool) != null)
1105             p.helpQuiescePool(w.workQueue, Long.MAX_VALUE, false);
1106         else
1107             ForkJoinPool.CommonPool.common.externalHelpQuiescePool(Long.MAX_VALUE, false);
1108     }
1109 
1110     /**
1111      * Resets the internal bookkeeping state of this task, allowing a
1112      * subsequent {@code fork}. This method allows repeated reuse of
1113      * this task, but only if reuse occurs when this task has either
1114      * never been forked, or has been forked, then completed and all
1115      * outstanding joins of this task have also completed. Effects
1116      * under any other usage conditions are not guaranteed.
1117      * This method may be useful when executing
1118      * pre-constructed trees of subtasks in loops.
1119      *
1120      * <p>Upon completion of this method, {@code isDone()} reports
1121      * {@code false}, and {@code getException()} reports {@code
1122      * null}. However, the value returned by {@code getRawResult} is
1123      * unaffected. To clear this value, you can invoke {@code
1124      * setRawResult(null)}.
1125      */
1126     public void reinitialize() {
1127         aux = null;
< prev index next >