1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  */
  22 
  23 /*
  24  * This file is available under and governed by the GNU General Public
  25  * License version 2 only, as published by the Free Software Foundation.
  26  * However, the following notice accompanied the original version of this
  27  * file:
  28  *
  29  * Written by Doug Lea with assistance from members of JCP JSR-166
  30  * Expert Group and released to the public domain, as explained at
  31  * http://creativecommons.org/publicdomain/zero/1.0/
  32  */
  33 
  34 /*
  35  * @test
  36  * @bug 4965960
  37  * @summary  Exercise ExecutorCompletionService
  38  * @library /test/lib
  39  */
  40 
  41 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  42 
  43 import java.util.concurrent.Callable;
  44 import java.util.concurrent.ExecutorCompletionService;
  45 import java.util.concurrent.ExecutorService;
  46 import java.util.concurrent.Executors;
  47 import jdk.test.lib.Utils;
  48 
  49 public class ExecutorCompletionServiceLoops {
  50     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
  51     static final int POOLSIZE = 10;
  52     static final ExecutorService pool =
  53         Executors.newFixedThreadPool(POOLSIZE);
  54     static final ExecutorCompletionService<Integer> ecs =
  55         new ExecutorCompletionService<>(pool);
  56     static boolean print = false;
  57 
  58     public static void main(String[] args) throws Exception {
  59         int max = 8;
  60         int base = 2000;
  61 
  62         if (args.length > 0)
  63             max = Integer.parseInt(args[0]);
  64 
  65         System.out.println("Warmup...");
  66         oneTest(base);
  67         print = true;
  68 
  69         for (int i = 1; i <= max; i += (i+1) >>> 1) {
  70             System.out.print("n: " + i * base);
  71             oneTest(i * base);
  72         }
  73         pool.shutdown();
  74         if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
  75             throw new Error();
  76    }
  77 
  78     static class Task implements Callable<Integer> {
  79         public Integer call() {
  80             int s = 0;
  81             int l = System.identityHashCode(this);
  82             for (int i = 0; i < 5; ++i) {
  83                 l = LoopHelpers.compute2(l);
  84                 s += LoopHelpers.compute1(l);
  85             }
  86             return new Integer(s);
  87         }
  88     }
  89 
  90     static class Producer implements Runnable {
  91         final ExecutorCompletionService cs;
  92         final int iters;
  93         Producer(ExecutorCompletionService ecs, int i) {
  94             cs = ecs;
  95             iters = i;
  96         }
  97         public void run() {
  98             for (int i = 0; i < iters; ++i)
  99                 ecs.submit(new Task());
 100         }
 101     }
 102 
 103     static void oneTest(int iters) throws Exception {
 104         long startTime = System.nanoTime();
 105         new Thread(new Producer(ecs, iters)).start();
 106 
 107         int r = 0;
 108         for (int i = 0; i < iters; ++i)
 109             r += ecs.take().get().intValue();
 110 
 111         long elapsed = System.nanoTime() - startTime;
 112         long tpi = elapsed/ iters;
 113 
 114         if (print)
 115             System.out.println("\t: " + LoopHelpers.rightJustify(tpi) + " ns per task");
 116 
 117         if (r == 0) // avoid overoptimization
 118             System.out.println("useless result: " + r);
 119     }
 120 
 121 }