jdk/src/java.base/share/classes/java/util/concurrent/AbstractExecutorService.java
author dl
Wed, 21 Dec 2016 14:26:52 -0800
changeset 42927 1d31e540bfcb
parent 32991 b27c76b82713
permissions -rw-r--r--
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12 Reviewed-by: martin, smarks, psandoz
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;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    37
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    38
import static java.util.concurrent.TimeUnit.NANOSECONDS;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    39
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    40
import java.util.ArrayList;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    41
import java.util.Collection;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    42
import java.util.Iterator;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    43
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Provides default implementations of {@link ExecutorService}
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    47
 * 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
    48
 * {@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
    49
 * {@link RunnableFuture} returned by {@code newTaskFor}, which defaults
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * 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
    51
 * 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
    52
 * associated {@code RunnableFuture} that is executed and
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    53
 * 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
    54
 * to return {@code RunnableFuture} implementations other than
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    55
 * {@code FutureTask}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    57
 * <p><b>Extension example</b>. Here is a sketch of a class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * that customizes {@link ThreadPoolExecutor} to use
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    59
 * a {@code CustomTask} class instead of the default {@code FutureTask}:
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    60
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    63
 *   static class CustomTask<V> implements RunnableFuture<V> {...}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    65
 *   protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    66
 *       return new CustomTask<V>(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    68
 *   protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    69
 *       return new CustomTask<V>(r, v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   // ... add constructors, etc.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    72
 * }}</pre>
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    73
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
public abstract class AbstractExecutorService implements ExecutorService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    80
     * Returns a {@code RunnableFuture} for the given runnable and default
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param runnable the runnable task being wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param value the default value for the returned future
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
    85
     * @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
    86
     * @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
    87
     * underlying runnable and which, as a {@code Future}, will yield
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * 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
    89
     * the underlying task
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return new FutureTask<T>(runnable, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    97
     * Returns a {@code RunnableFuture} for the given callable task.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param callable the callable task being wrapped
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
   100
     * @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
   101
     * @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
   102
     * underlying callable and which, as a {@code Future}, will yield
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * 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
   104
     * cancellation of the underlying task
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return new FutureTask<T>(callable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public Future<?> submit(Runnable task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (task == null) throw new NullPointerException();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   117
        RunnableFuture<Void> ftask = newTaskFor(task, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        execute(ftask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return ftask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public <T> Future<T> submit(Runnable task, T result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (task == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        RunnableFuture<T> ftask = newTaskFor(task, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        execute(ftask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return ftask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public <T> Future<T> submit(Callable<T> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (task == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        RunnableFuture<T> ftask = newTaskFor(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        execute(ftask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return ftask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the main mechanics of invokeAny.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    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
   148
                              boolean timed, long nanos)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        throws InterruptedException, ExecutionException, TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (tasks == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int ntasks = tasks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (ntasks == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   155
        ArrayList<Future<T>> futures = new ArrayList<>(ntasks);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        ExecutorCompletionService<T> ecs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            new ExecutorCompletionService<T>(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // For efficiency, especially in executors with limited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // parallelism, check to see if previously submitted tasks are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // done before submitting more of them. This interleaving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // plus the exception mechanics account for messiness of main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // Record exceptions so that if we fail to obtain any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // result, we can throw the last exception we got.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            ExecutionException ee = null;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   169
            final long deadline = timed ? System.nanoTime() + nanos : 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Iterator<? extends Callable<T>> it = tasks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // Start one task for sure; the rest incrementally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            futures.add(ecs.submit(it.next()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            --ntasks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int active = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                Future<T> f = ecs.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    if (ntasks > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        --ntasks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        futures.add(ecs.submit(it.next()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        ++active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    else if (active == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    else if (timed) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   188
                        f = ecs.poll(nanos, NANOSECONDS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        if (f == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            throw new TimeoutException();
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   191
                        nanos = deadline - System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        f = ecs.take();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    --active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        return f.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    } catch (ExecutionException eex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        ee = eex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    } catch (RuntimeException rex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        ee = new ExecutionException(rex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (ee == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                ee = new ExecutionException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } finally {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   213
            cancelAll(futures);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        throws InterruptedException, ExecutionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return doInvokeAny(tasks, false, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch (TimeoutException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                           long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throws InterruptedException, ExecutionException, TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return doInvokeAny(tasks, true, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (tasks == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   237
        ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            for (Callable<T> t : tasks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                RunnableFuture<T> f = newTaskFor(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                futures.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                execute(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   244
            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
   245
                Future<T> f = futures.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                if (!f.isDone()) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   247
                    try { f.get(); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   248
                    catch (CancellationException ignore) {}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   249
                    catch (ExecutionException ignore) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return futures;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   253
        } catch (Throwable t) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   254
            cancelAll(futures);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   255
            throw t;
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();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   264
        final long nanos = unit.toNanos(timeout);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   265
        final long deadline = System.nanoTime() + nanos;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   266
        ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   267
        int j = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   268
        timedOut: try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            for (Callable<T> t : tasks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                futures.add(newTaskFor(t));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
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++) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   277
                if (((i == 0) ? nanos : deadline - System.nanoTime()) <= 0L)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   278
                    break timedOut;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   279
                execute((Runnable)futures.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   282
            for (; j < size; j++) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   283
                Future<T> f = futures.get(j);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if (!f.isDone()) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   285
                    try { f.get(deadline - System.nanoTime(), NANOSECONDS); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   286
                    catch (CancellationException ignore) {}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   287
                    catch (ExecutionException ignore) {}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   288
                    catch (TimeoutException timedOut) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   289
                        break timedOut;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return futures;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   294
        } catch (Throwable t) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   295
            cancelAll(futures);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   296
            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   298
        // Timed out before all the tasks could be completed; cancel remaining
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   299
        cancelAll(futures, j);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   300
        return futures;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   303
    private static <T> void cancelAll(ArrayList<Future<T>> futures) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   304
        cancelAll(futures, 0);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   305
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   306
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   307
    /** Cancels all futures with index at least j. */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   308
    private static <T> void cancelAll(ArrayList<Future<T>> futures, int j) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   309
        for (int size = futures.size(); j < size; j++)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   310
            futures.get(j).cancel(true);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   311
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}