0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file:
0N/A *
0N/A * Written by Doug Lea with assistance from members of JCP JSR-166
0N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4965960
3203N/A * @compile -source 1.5 ExecutorCompletionServiceLoops.java
0N/A * @run main/timeout=3600 ExecutorCompletionServiceLoops
0N/A * @summary Exercise ExecutorCompletionServiceLoops
0N/A */
0N/A
0N/Aimport java.util.concurrent.*;
0N/A
0N/Apublic class ExecutorCompletionServiceLoops {
0N/A static final int POOLSIZE = 100;
0N/A static final ExecutorService pool =
0N/A Executors.newFixedThreadPool(POOLSIZE);
0N/A static final ExecutorCompletionService<Integer> ecs =
0N/A new ExecutorCompletionService<Integer>(pool);
0N/A static boolean print = false;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A int max = 8;
0N/A int base = 10000;
0N/A
0N/A if (args.length > 0)
0N/A max = Integer.parseInt(args[0]);
0N/A
0N/A System.out.println("Warmup...");
0N/A oneTest( base );
0N/A Thread.sleep(100);
0N/A print = true;
0N/A
0N/A for (int i = 1; i <= max; i += (i+1) >>> 1) {
0N/A System.out.print("n: " + i * base);
0N/A oneTest(i * base );
0N/A Thread.sleep(100);
0N/A }
0N/A pool.shutdown();
0N/A if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
0N/A throw new Error();
0N/A }
0N/A
0N/A static class Task implements Callable<Integer> {
0N/A public Integer call() {
0N/A int s = 0;
0N/A int l = System.identityHashCode(this);
0N/A for (int i = 0; i < 5; ++i) {
0N/A l = LoopHelpers.compute2(l);
0N/A s += LoopHelpers.compute1(l);
0N/A }
0N/A return new Integer(s);
0N/A }
0N/A }
0N/A
0N/A static class Producer implements Runnable {
0N/A final ExecutorCompletionService cs;
0N/A final int iters;
0N/A Producer(ExecutorCompletionService ecs, int i) {
0N/A cs = ecs;
0N/A iters = i;
0N/A }
0N/A public void run() {
0N/A for (int i = 0; i < iters; ++i)
0N/A ecs.submit(new Task());
0N/A }
0N/A }
0N/A
0N/A static void oneTest(int iters) throws Exception {
0N/A long startTime = System.nanoTime();
0N/A new Thread(new Producer(ecs, iters)).start();
0N/A
0N/A int r = 0;
0N/A for (int i = 0; i < iters; ++i)
0N/A r += ecs.take().get().intValue();
0N/A
0N/A long elapsed = System.nanoTime() - startTime;
0N/A long tpi = elapsed/ iters;
0N/A
0N/A if (print)
0N/A System.out.println("\t: " + LoopHelpers.rightJustify(tpi) + " ns per task");
0N/A
0N/A if (r == 0) // avoid overoptimization
0N/A System.out.println("useless result: " + r);
0N/A
0N/A
0N/A }
0N/A
0N/A}