blob: fe713fe0791004197204ef2bc55a3756b39e08e0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
19 * CA 95054 USA or visit www.sun.com if you need additional information or
20 * have any 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/licenses/publicdomain
32 */
33
34/*
35 * @test
36 * @bug 4965960
37 * @compile -source 1.5 ExecutorCompletionServiceLoops.java
38 * @run main/timeout=3600 ExecutorCompletionServiceLoops
39 * @summary Exercise ExecutorCompletionServiceLoops
40 */
41
42import java.util.concurrent.*;
43
44public class ExecutorCompletionServiceLoops {
45 static final int POOLSIZE = 100;
46 static final ExecutorService pool =
47 Executors.newFixedThreadPool(POOLSIZE);
48 static final ExecutorCompletionService<Integer> ecs =
49 new ExecutorCompletionService<Integer>(pool);
50 static boolean print = false;
51
52 public static void main(String[] args) throws Exception {
53 int max = 8;
54 int base = 10000;
55
56 if (args.length > 0)
57 max = Integer.parseInt(args[0]);
58
59 System.out.println("Warmup...");
60 oneTest( base );
61 Thread.sleep(100);
62 print = true;
63
64 for (int i = 1; i <= max; i += (i+1) >>> 1) {
65 System.out.print("n: " + i * base);
66 oneTest(i * base );
67 Thread.sleep(100);
68 }
69 pool.shutdown();
70 if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
71 throw new Error();
72 }
73
74 static class Task implements Callable<Integer> {
75 public Integer call() {
76 int s = 0;
77 int l = System.identityHashCode(this);
78 for (int i = 0; i < 5; ++i) {
79 l = LoopHelpers.compute2(l);
80 s += LoopHelpers.compute1(l);
81 }
82 return new Integer(s);
83 }
84 }
85
86 static class Producer implements Runnable {
87 final ExecutorCompletionService cs;
88 final int iters;
89 Producer(ExecutorCompletionService ecs, int i) {
90 cs = ecs;
91 iters = i;
92 }
93 public void run() {
94 for (int i = 0; i < iters; ++i)
95 ecs.submit(new Task());
96 }
97 }
98
99 static void oneTest(int iters) throws Exception {
100 long startTime = System.nanoTime();
101 new Thread(new Producer(ecs, iters)).start();
102
103 int r = 0;
104 for (int i = 0; i < iters; ++i)
105 r += ecs.take().get().intValue();
106
107 long elapsed = System.nanoTime() - startTime;
108 long tpi = elapsed/ iters;
109
110 if (print)
111 System.out.println("\t: " + LoopHelpers.rightJustify(tpi) + " ns per task");
112
113 if (r == 0) // avoid overoptimization
114 System.out.println("useless result: " + r);
115
116
117 }
118
119}