jdk/src/java.base/share/classes/java/util/concurrent/AbstractExecutorService.java
author zmajo
Fri, 03 Jul 2015 07:23:45 +0200
changeset 31671 362e0c0acece
parent 25859 3317bb8137f4
child 32991 b27c76b82713
permissions -rw-r--r--
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics Summary: Annotate possibly intrinsified methods with @HotSpotIntrinsicCandidate. Add checks omitted by intrinsics to the library code. Add CheckIntrinsics flags to check consistency of intrinsics. Reviewed-by: jrose, kvn, thartmann, vlivanov, abuckley, darcy, ascarpino, briangoetz, alanb, aph, dnsimon
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Provides default implementations of {@link ExecutorService}
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    41
 * execution methods. This class implements the {@code submit},
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    42
 * {@code invokeAny} and {@code invokeAll} methods using a
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    43
 * {@link RunnableFuture} returned by {@code newTaskFor}, which defaults
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to the {@link FutureTask} class provided in this package.  For example,
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    45
 * the implementation of {@code submit(Runnable)} creates an
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    46
 * associated {@code RunnableFuture} that is executed and
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    47
 * returned. Subclasses may override the {@code newTaskFor} methods
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    48
 * to return {@code RunnableFuture} implementations other than
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    49
 * {@code FutureTask}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    51
 * <p><b>Extension example</b>. Here is a sketch of a class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * that customizes {@link ThreadPoolExecutor} to use
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    53
 * a {@code CustomTask} class instead of the default {@code FutureTask}:
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    54
 *  <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    57
 *   static class CustomTask<V> implements RunnableFuture<V> {...}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    59
 *   protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    60
 *       return new CustomTask<V>(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    62
 *   protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    63
 *       return new CustomTask<V>(r, v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   // ... add constructors, etc.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    66
 * }}</pre>
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    67
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public abstract class AbstractExecutorService implements ExecutorService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    74
     * Returns a {@code RunnableFuture} for the given runnable and default
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param runnable the runnable task being wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param value the default value for the returned future
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
    79
     * @param <T> the type of the given value
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    80
     * @return a {@code RunnableFuture} which, when run, will run the
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    81
     * underlying runnable and which, as a {@code Future}, will yield
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * the given value as its result and provide for cancellation of
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    83
     * the underlying task
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return new FutureTask<T>(runnable, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    91
     * Returns a {@code RunnableFuture} for the given callable task.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param callable the callable task being wrapped
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
    94
     * @param <T> the type of the callable's result
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    95
     * @return a {@code RunnableFuture} which, when run, will call the
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    96
     * underlying callable and which, as a {@code Future}, will yield
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * the callable's result as its result and provide for
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    98
     * cancellation of the underlying task
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return new FutureTask<T>(callable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public Future<?> submit(Runnable task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (task == null) throw new NullPointerException();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   111
        RunnableFuture<Void> ftask = newTaskFor(task, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        execute(ftask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return ftask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public <T> Future<T> submit(Runnable task, T result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (task == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        RunnableFuture<T> ftask = newTaskFor(task, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        execute(ftask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return ftask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public <T> Future<T> submit(Callable<T> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (task == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        RunnableFuture<T> ftask = newTaskFor(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        execute(ftask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return ftask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * the main mechanics of invokeAny.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   142
                              boolean timed, long nanos)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        throws InterruptedException, ExecutionException, TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (tasks == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int ntasks = tasks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (ntasks == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new IllegalArgumentException();
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   149
        ArrayList<Future<T>> futures = new ArrayList<Future<T>>(ntasks);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ExecutorCompletionService<T> ecs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            new ExecutorCompletionService<T>(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // For efficiency, especially in executors with limited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // parallelism, check to see if previously submitted tasks are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // done before submitting more of them. This interleaving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // plus the exception mechanics account for messiness of main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // Record exceptions so that if we fail to obtain any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // result, we can throw the last exception we got.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            ExecutionException ee = null;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   163
            final long deadline = timed ? System.nanoTime() + nanos : 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            Iterator<? extends Callable<T>> it = tasks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // Start one task for sure; the rest incrementally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            futures.add(ecs.submit(it.next()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            --ntasks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            int active = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                Future<T> f = ecs.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    if (ntasks > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        --ntasks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        futures.add(ecs.submit(it.next()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        ++active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    else if (active == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    else if (timed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        if (f == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            throw new TimeoutException();
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   185
                        nanos = deadline - System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        f = ecs.take();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    --active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        return f.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    } catch (ExecutionException eex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        ee = eex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    } catch (RuntimeException rex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        ee = new ExecutionException(rex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (ee == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                ee = new ExecutionException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } finally {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   207
            for (int i = 0, size = futures.size(); i < size; i++)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   208
                futures.get(i).cancel(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throws InterruptedException, ExecutionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return doInvokeAny(tasks, false, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } catch (TimeoutException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                           long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throws InterruptedException, ExecutionException, TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return doInvokeAny(tasks, true, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (tasks == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new NullPointerException();
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   232
        ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            for (Callable<T> t : tasks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                RunnableFuture<T> f = newTaskFor(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                futures.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                execute(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   240
            for (int i = 0, size = futures.size(); i < size; i++) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   241
                Future<T> f = futures.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                if (!f.isDone()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        f.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    } catch (CancellationException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    } catch (ExecutionException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return futures;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (!done)
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   254
                for (int i = 0, size = futures.size(); i < size; i++)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   255
                    futures.get(i).cancel(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                         long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        throws InterruptedException {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   262
        if (tasks == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        long nanos = unit.toNanos(timeout);
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   265
        ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            for (Callable<T> t : tasks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                futures.add(newTaskFor(t));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   271
            final long deadline = System.nanoTime() + nanos;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   272
            final int size = futures.size();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // Interleave time checks and calls to execute in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // executor doesn't have any/much parallelism.
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   276
            for (int i = 0; i < size; i++) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   277
                execute((Runnable)futures.get(i));
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   278
                nanos = deadline - System.nanoTime();
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   279
                if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    return futures;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   283
            for (int i = 0; i < size; i++) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   284
                Future<T> f = futures.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (!f.isDone()) {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   286
                    if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        return futures;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        f.get(nanos, TimeUnit.NANOSECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    } catch (CancellationException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    } catch (ExecutionException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    } catch (TimeoutException toe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        return futures;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   295
                    nanos = deadline - System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return futures;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (!done)
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   302
                for (int i = 0, size = futures.size(); i < size; i++)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   303
                    futures.get(i).cancel(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}