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/licenses/publicdomain
32 */
33
34 /*
35 * @test
36 * @bug 4486658
37 * @compile ExchangeLoops.java
38 * @run main/timeout=720 ExchangeLoops
39 * @summary checks to make sure a pipeline of exchangers passes data.
40 */
41
42 import java.util.concurrent.*;
43
44 public class ExchangeLoops {
45 static final ExecutorService pool = Executors.newCachedThreadPool();
46 static boolean print = false;
47
48 static class Int {
49 public int value;
50 Int(int i) { value = i; }
51 }
52
53
54 public static void main(String[] args) throws Exception {
55 int maxStages = 5;
56 int iters = 10000;
57
61 print = false;
62 System.out.println("Warmup...");
63 oneRun(2, 100000);
64 print = true;
65
66 for (int i = 2; i <= maxStages; i += (i+1) >>> 1) {
67 System.out.print("Threads: " + i + "\t: ");
68 oneRun(i, iters);
69 }
70 pool.shutdown();
71 if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
72 throw new Error();
73 }
74
75 static class Stage implements Runnable {
76 final int iters;
77 final Exchanger<Int> left;
78 final Exchanger<Int> right;
79 final CyclicBarrier barrier;
80 volatile int result;
81 Stage (Exchanger<Int> left,
82 Exchanger<Int> right,
83 CyclicBarrier b, int iters) {
84 this.left = left;
85 this.right = right;
86 barrier = b;
87 this.iters = iters;
88 }
89
90 public void run() {
91 try {
92 barrier.await();
93 Int item = new Int(hashCode());
94 for (int i = 0; i < iters; ++i) {
95 if (left != null) {
96 item.value = LoopHelpers.compute1(item.value);
97 Int other = left.exchange(item);
98 if (other == item || other == null)
99 throw new Error("Failed Exchange");
100 item = other;
101
|
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/licenses/publicdomain
32 */
33
34 /*
35 * @test
36 * @bug 4486658
37 * @compile -source 1.5 ExchangeLoops.java
38 * @run main/timeout=720 ExchangeLoops
39 * @summary checks to make sure a pipeline of exchangers passes data.
40 */
41
42 import java.util.concurrent.*;
43
44 public class ExchangeLoops {
45 static final ExecutorService pool = Executors.newCachedThreadPool();
46 static boolean print = false;
47
48 static class Int {
49 public int value;
50 Int(int i) { value = i; }
51 }
52
53
54 public static void main(String[] args) throws Exception {
55 int maxStages = 5;
56 int iters = 10000;
57
61 print = false;
62 System.out.println("Warmup...");
63 oneRun(2, 100000);
64 print = true;
65
66 for (int i = 2; i <= maxStages; i += (i+1) >>> 1) {
67 System.out.print("Threads: " + i + "\t: ");
68 oneRun(i, iters);
69 }
70 pool.shutdown();
71 if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
72 throw new Error();
73 }
74
75 static class Stage implements Runnable {
76 final int iters;
77 final Exchanger<Int> left;
78 final Exchanger<Int> right;
79 final CyclicBarrier barrier;
80 volatile int result;
81 Stage(Exchanger<Int> left,
82 Exchanger<Int> right,
83 CyclicBarrier b, int iters) {
84 this.left = left;
85 this.right = right;
86 barrier = b;
87 this.iters = iters;
88 }
89
90 public void run() {
91 try {
92 barrier.await();
93 Int item = new Int(hashCode());
94 for (int i = 0; i < iters; ++i) {
95 if (left != null) {
96 item.value = LoopHelpers.compute1(item.value);
97 Int other = left.exchange(item);
98 if (other == item || other == null)
99 throw new Error("Failed Exchange");
100 item = other;
101
|