jdk/test/java/util/concurrent/ExecutorCompletionService/ExecutorCompletionServiceLoops.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4347 ab0a9f495844
child 7518 0282db800fe1
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
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
4347
ab0a9f495844 6907177: Update jdk tests to remove unncessary -source and -target options
darcy
parents: 2
diff changeset
    37
 * @compile ExecutorCompletionServiceLoops.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/timeout=3600 ExecutorCompletionServiceLoops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @summary  Exercise ExecutorCompletionServiceLoops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class ExecutorCompletionServiceLoops {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int POOLSIZE =      100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final ExecutorService pool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Executors.newFixedThreadPool(POOLSIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final ExecutorCompletionService<Integer> ecs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        new ExecutorCompletionService<Integer>(pool);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static boolean print = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int max = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        int base = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            max = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.out.println("Warmup...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        oneTest( base );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        print = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (int i = 1; i <= max; i += (i+1) >>> 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.print("n: " + i * base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            oneTest(i * base );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static class Task implements Callable<Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public Integer call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            int s = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            int l = System.identityHashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            for (int i = 0; i < 5; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                l = LoopHelpers.compute2(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                s += LoopHelpers.compute1(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return new Integer(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static class Producer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        final ExecutorCompletionService cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        final int iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Producer(ExecutorCompletionService ecs, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            cs = ecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            iters = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            for (int i = 0; i < iters; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                ecs.submit(new Task());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static void oneTest(int iters) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        long startTime = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        new Thread(new Producer(ecs, iters)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int r = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (int i = 0; i < iters; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            r += ecs.take().get().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        long elapsed = System.nanoTime() - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        long tpi = elapsed/ iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (print)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.out.println("\t: " + LoopHelpers.rightJustify(tpi) + " ns per task");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (r == 0) // avoid overoptimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.out.println("useless result: " + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}