test/jdk/java/util/concurrent/FutureTask/CancelledFutureLoops.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 51754 594919232b8f
permissions -rw-r--r--
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09 Reviewed-by: martin, alanb
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * 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
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @bug 4486658
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @summary Checks for responsiveness of futures to cancellation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Runs under the assumption that ITERS computations require more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * TIMEOUT msecs to complete.
51754
594919232b8f 8210732: remove jdk.testlibrary.Utils
iignatyev
parents: 47216
diff changeset
    40
 * @library /test/lib
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    41
 * @run main/timeout=2000 CancelledFutureLoops
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    44
import static java.util.concurrent.TimeUnit.MILLISECONDS;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    45
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    46
import java.util.SplittableRandom;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    47
import java.util.concurrent.BrokenBarrierException;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    48
import java.util.concurrent.Callable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    49
import java.util.concurrent.CyclicBarrier;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    50
import java.util.concurrent.ExecutionException;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    51
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    52
import java.util.concurrent.Executors;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    53
import java.util.concurrent.Future;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    54
import java.util.concurrent.locks.ReentrantLock;
51754
594919232b8f 8210732: remove jdk.testlibrary.Utils
iignatyev
parents: 47216
diff changeset
    55
import jdk.test.lib.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public final class CancelledFutureLoops {
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    58
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final ExecutorService pool = Executors.newCachedThreadPool();
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    60
    static final SplittableRandom rnd = new SplittableRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static boolean print = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final int ITERS = 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final long TIMEOUT = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int maxThreads = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            maxThreads = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        print = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.out.print("Threads: " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            try {
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    75
                new FutureLoop(i, rnd.split()).test();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    77
            catch (BrokenBarrierException bb) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                // OK; ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    80
            catch (ExecutionException ee) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                // OK; ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Thread.sleep(TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        pool.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    86
        if (! pool.awaitTermination(6 * LONG_DELAY_MS, MILLISECONDS))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final class FutureLoop implements Callable {
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    91
        private final int nthreads;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    92
        private final SplittableRandom rnd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        private final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        private final CyclicBarrier barrier;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    96
        private int v;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    97
        FutureLoop(int nthreads, SplittableRandom rnd) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            this.nthreads = nthreads;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
    99
            this.rnd = rnd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            barrier = new CyclicBarrier(nthreads+1, timer);
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   101
            v = rnd.nextInt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        final void test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            Future[] futures = new Future[nthreads];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            for (int i = 0; i < nthreads; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                futures[i] = pool.submit(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Thread.sleep(TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            boolean tooLate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            for (int i = 1; i < nthreads; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (!futures[i].cancel(true))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    tooLate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // Unbunch some of the cancels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if ( (i & 3) == 0)
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 22293
diff changeset
   117
                    Thread.sleep(1 + rnd.nextInt(5));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            Object f0 = futures[0].get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (!tooLate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                for (int i = 1; i < nthreads; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    if (!futures[i].isDone() || !futures[i].isCancelled())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        throw new Error("Only one thread should complete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                System.out.print("(cancelled too late) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            long endTime = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            long time = endTime - timer.startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (print) {
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 51754
diff changeset
   133
                double secs = (double)time / 1000000000.0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                System.out.println("\t " + secs + "s run time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
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
        public final Object call() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int sum = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int n = ITERS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            while (n-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    v = x = LoopHelpers.compute1(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                sum += LoopHelpers.compute2(LoopHelpers.compute2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return new Integer(sum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}