test/jdk/java/util/concurrent/ExecutorCompletionService/ExecutorCompletionServiceLoops.java
author dl
Tue, 16 Jan 2018 18:28:39 -0800
changeset 48541 946e34c2dec9
parent 47216 71c04702a3d5
child 51754 594919232b8f
permissions -rw-r--r--
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01 Reviewed-by: martin
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 4965960
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 35981
diff changeset
    37
 * @summary  Exercise ExecutorCompletionService
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 35981
diff changeset
    38
 * @library /lib/testlibrary/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 35981
diff changeset
    41
import static java.util.concurrent.TimeUnit.MILLISECONDS;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    42
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    43
import java.util.concurrent.Callable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    44
import java.util.concurrent.ExecutorCompletionService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    45
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    46
import java.util.concurrent.Executors;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 35981
diff changeset
    47
import jdk.testlibrary.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class ExecutorCompletionServiceLoops {
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 35981
diff changeset
    50
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 35981
diff changeset
    51
    static final int POOLSIZE = 10;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final ExecutorService pool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Executors.newFixedThreadPool(POOLSIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final ExecutorCompletionService<Integer> ecs =
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    55
        new ExecutorCompletionService<>(pool);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static boolean print = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int max = 8;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 35981
diff changeset
    60
        int base = 2000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            max = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println("Warmup...");
35981
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 34347
diff changeset
    66
        oneTest(base);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        print = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        for (int i = 1; i <= max; i += (i+1) >>> 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            System.out.print("n: " + i * base);
35981
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 34347
diff changeset
    71
            oneTest(i * base);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        pool.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 35981
diff changeset
    74
        if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static class Task implements Callable<Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public Integer call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            int s = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int l = System.identityHashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            for (int i = 0; i < 5; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                l = LoopHelpers.compute2(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                s += LoopHelpers.compute1(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return new Integer(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
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 class Producer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        final ExecutorCompletionService cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        final int iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Producer(ExecutorCompletionService ecs, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            cs = ecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            iters = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            for (int i = 0; i < iters; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                ecs.submit(new Task());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static void oneTest(int iters) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        long startTime = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        new Thread(new Producer(ecs, iters)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int r = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (int i = 0; i < iters; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            r += ecs.take().get().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        long elapsed = System.nanoTime() - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        long tpi = elapsed/ iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (print)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println("\t: " + LoopHelpers.rightJustify(tpi) + " ns per task");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (r == 0) // avoid overoptimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.out.println("useless result: " + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}