Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/Exchanger/ExchangeLoops.java
+++ new/test/java/util/concurrent/Exchanger/ExchangeLoops.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 4486658
37 - * @compile ExchangeLoops.java
37 + * @compile -source 1.5 ExchangeLoops.java
38 38 * @run main/timeout=720 ExchangeLoops
39 39 * @summary checks to make sure a pipeline of exchangers passes data.
40 40 */
41 41
42 42 import java.util.concurrent.*;
43 43
44 44 public class ExchangeLoops {
45 45 static final ExecutorService pool = Executors.newCachedThreadPool();
46 46 static boolean print = false;
47 47
48 48 static class Int {
49 49 public int value;
50 50 Int(int i) { value = i; }
51 51 }
52 52
53 53
54 54 public static void main(String[] args) throws Exception {
55 55 int maxStages = 5;
56 56 int iters = 10000;
57 57
58 58 if (args.length > 0)
59 59 maxStages = Integer.parseInt(args[0]);
60 60
61 61 print = false;
62 62 System.out.println("Warmup...");
63 63 oneRun(2, 100000);
64 64 print = true;
65 65
66 66 for (int i = 2; i <= maxStages; i += (i+1) >>> 1) {
67 67 System.out.print("Threads: " + i + "\t: ");
68 68 oneRun(i, iters);
69 69 }
70 70 pool.shutdown();
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
71 71 if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
72 72 throw new Error();
73 73 }
74 74
75 75 static class Stage implements Runnable {
76 76 final int iters;
77 77 final Exchanger<Int> left;
78 78 final Exchanger<Int> right;
79 79 final CyclicBarrier barrier;
80 80 volatile int result;
81 - Stage (Exchanger<Int> left,
82 - Exchanger<Int> right,
83 - CyclicBarrier b, int iters) {
81 + Stage(Exchanger<Int> left,
82 + Exchanger<Int> right,
83 + CyclicBarrier b, int iters) {
84 84 this.left = left;
85 85 this.right = right;
86 86 barrier = b;
87 87 this.iters = iters;
88 88 }
89 89
90 90 public void run() {
91 91 try {
92 92 barrier.await();
93 93 Int item = new Int(hashCode());
94 94 for (int i = 0; i < iters; ++i) {
95 95 if (left != null) {
96 96 item.value = LoopHelpers.compute1(item.value);
97 97 Int other = left.exchange(item);
98 98 if (other == item || other == null)
99 99 throw new Error("Failed Exchange");
100 100 item = other;
101 101
102 102 }
103 103 if (right != null) {
104 104 item.value = LoopHelpers.compute2(item.value);
105 105 Int other = right.exchange(item);
106 106 if (other == item || other == null)
107 107 throw new Error("Failed Exchange");
108 108 item = other;
109 109 }
110 110 }
111 111 barrier.await();
112 112
113 113 }
114 114 catch (Exception ie) {
115 115 ie.printStackTrace();
116 116 return;
117 117 }
118 118 }
119 119 }
120 120
121 121 static void oneRun(int nthreads, int iters) throws Exception {
122 122 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
123 123 CyclicBarrier barrier = new CyclicBarrier(nthreads + 1, timer);
124 124 Exchanger<Int> l = null;
125 125 Exchanger<Int> r = new Exchanger<Int>();
126 126 for (int i = 0; i < nthreads; ++i) {
127 127 pool.execute(new Stage(l, r, barrier, iters));
128 128 l = r;
129 129 r = (i+2 < nthreads) ? new Exchanger<Int>() : null;
130 130 }
131 131 barrier.await();
132 132 barrier.await();
133 133 long time = timer.getTime();
134 134 if (print)
135 135 System.out.println(LoopHelpers.rightJustify(time / (iters * nthreads + iters * (nthreads-2))) + " ns per transfer");
136 136 }
137 137
138 138 }
↓ open down ↓ |
45 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX