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