test/jdk/java/util/concurrent/ExecutorService/Invoke.java
author dl
Fri, 13 Oct 2017 18:12:54 -0700
changeset 47341 ed1fd45b6eb5
parent 47216 71c04702a3d5
child 47727 53020d8cdf5b
permissions -rw-r--r--
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure Reviewed-by: martin, psandoz, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     6267833
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests for invokeAny, invokeAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    31
import static java.util.concurrent.TimeUnit.NANOSECONDS;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    32
import static java.util.concurrent.TimeUnit.SECONDS;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    33
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    34
import java.util.List;
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    35
import java.util.stream.Collectors;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    36
import java.util.stream.IntStream;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    37
import java.util.concurrent.Callable;
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    38
import java.util.concurrent.CyclicBarrier;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    39
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    40
import java.util.concurrent.Executors;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    41
import java.util.concurrent.Future;
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    42
import java.util.concurrent.ThreadLocalRandom;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    43
import java.util.concurrent.atomic.AtomicLong;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class Invoke {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static void fail(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        failed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        new AssertionError(msg).printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static void pass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        passed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static void unexpected(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        failed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static void check(boolean condition, String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (condition) pass(); else fail(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static void check(boolean condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        check(condition, "Assertion failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    70
    static long secondsElapsedSince(long startTime) {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    71
        return NANOSECONDS.toSeconds(System.nanoTime() - startTime);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    72
    }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    73
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    74
    static void awaitInterrupt(long timeoutSeconds) {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    75
        long startTime = System.nanoTime();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    76
        try {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    77
            Thread.sleep(SECONDS.toMillis(timeoutSeconds));
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    78
            fail("timed out waiting for interrupt");
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    79
        } catch (InterruptedException expected) {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    80
            check(secondsElapsedSince(startTime) < timeoutSeconds);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    81
        }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    82
    }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    83
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    86
            testInvokeAll();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    87
            testInvokeAny();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    88
            testInvokeAny_cancellationInterrupt();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    89
        } catch (Throwable t) {  unexpected(t); }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    90
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    91
        if (failed > 0)
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    92
            throw new Error(
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    93
                    String.format("Passed = %d, failed = %d", passed, failed));
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    94
    }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    95
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    96
    static final long timeoutSeconds = 10L;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    97
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    98
    static void testInvokeAll() throws Throwable {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
    99
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   100
        final int nThreads = rnd.nextInt(2, 7);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   101
        final boolean timed = rnd.nextBoolean();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   102
        final ExecutorService pool = Executors.newFixedThreadPool(nThreads);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   103
        final AtomicLong count = new AtomicLong(0);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   104
        class Task implements Callable<Long> {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   105
            public Long call() throws Exception {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   106
                return count.incrementAndGet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   108
        }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   109
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   110
        try {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   111
            final List<Task> tasks =
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   112
                IntStream.range(0, nThreads)
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   113
                .mapToObj(i -> new Task())
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   114
                .collect(Collectors.toList());
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   115
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   116
            List<Future<Long>> futures;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   117
            if (timed) {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   118
                long startTime = System.nanoTime();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   119
                futures = pool.invokeAll(tasks, timeoutSeconds, SECONDS);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   120
                check(secondsElapsedSince(startTime) < timeoutSeconds);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   121
            }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   122
            else
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   123
                futures = pool.invokeAll(tasks);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            check(futures.size() == tasks.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            check(count.get() == tasks.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            long gauss = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            for (Future<Long> future : futures) gauss += future.get();
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   129
            check(gauss == (tasks.size()+1)*tasks.size()/2);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   130
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   131
            pool.shutdown();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   132
            check(pool.awaitTermination(10L, SECONDS));
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   133
        } finally {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   134
            pool.shutdownNow();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   135
        }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   136
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   138
    static void testInvokeAny() throws Throwable {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   139
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   140
        final boolean timed = rnd.nextBoolean();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   141
        final ExecutorService pool = Executors.newSingleThreadExecutor();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   142
        final AtomicLong count = new AtomicLong(0);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   143
        class Task implements Callable<Long> {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   144
            public Long call() throws Exception {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   145
                long x = count.incrementAndGet();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   146
                check(x <= 2);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   147
                if (x == 2)
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   148
                    // wait for main thread to interrupt us
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   149
                    awaitInterrupt(timeoutSeconds);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   150
                return x;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   151
            }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   152
        }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   153
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   154
        try {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   155
            final List<Task> tasks =
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   156
                IntStream.range(0, rnd.nextInt(1, 7))
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   157
                .mapToObj(i -> new Task())
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   158
                .collect(Collectors.toList());
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   159
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   160
            long val;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   161
            if (timed) {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   162
                long startTime = System.nanoTime();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   163
                val = pool.invokeAny(tasks, timeoutSeconds, SECONDS);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   164
                check(secondsElapsedSince(startTime) < timeoutSeconds);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   165
            }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   166
            else
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   167
                val = pool.invokeAny(tasks);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   168
            check(val == 1);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   169
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   170
            // inherent race between main thread interrupt and
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   171
            // start of second task
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   172
            check(count.get() == 1 || count.get() == 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   174
            pool.shutdown();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   175
            check(pool.awaitTermination(timeoutSeconds, SECONDS));
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   176
        } finally {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   177
            pool.shutdownNow();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   178
        }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   179
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   181
    /**
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   182
     * Every remaining running task is sent an interrupt for cancellation.
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   183
     */
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   184
    static void testInvokeAny_cancellationInterrupt() throws Throwable {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   185
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   186
        final int nThreads = rnd.nextInt(2, 7);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   187
        final boolean timed = rnd.nextBoolean();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   188
        final ExecutorService pool = Executors.newFixedThreadPool(nThreads);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   189
        final AtomicLong count = new AtomicLong(0);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   190
        final AtomicLong interruptedCount = new AtomicLong(0);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   191
        final CyclicBarrier allStarted = new CyclicBarrier(nThreads);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   192
        class Task implements Callable<Long> {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   193
            public Long call() throws Exception {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   194
                allStarted.await();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   195
                long x = count.incrementAndGet();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   196
                if (x > 1)
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   197
                    // main thread will interrupt us
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   198
                    awaitInterrupt(timeoutSeconds);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   199
                return x;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   200
            }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   201
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
47341
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   203
        try {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   204
            final List<Task> tasks =
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   205
                IntStream.range(0, nThreads)
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   206
                .mapToObj(i -> new Task())
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   207
                .collect(Collectors.toList());
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   208
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   209
            long val;
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   210
            if (timed) {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   211
                long startTime = System.nanoTime();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   212
                val = pool.invokeAny(tasks, timeoutSeconds, SECONDS);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   213
                check(secondsElapsedSince(startTime) < timeoutSeconds);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   214
            }
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   215
            else
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   216
                val = pool.invokeAny(tasks);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   217
            check(val == 1);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   218
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   219
            pool.shutdown();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   220
            check(pool.awaitTermination(timeoutSeconds, SECONDS));
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   221
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   222
            // Check after shutdown to avoid race
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   223
            check(count.get() == nThreads);
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   224
        } finally {
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   225
            pool.shutdownNow();
ed1fd45b6eb5 8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents: 47216
diff changeset
   226
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
}