Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/FutureTask/CancelledFutureLoops.java
+++ new/test/java/util/concurrent/FutureTask/CancelledFutureLoops.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 CancelledFutureLoops.java
37 + * @compile -source 1.5 CancelledFutureLoops.java
38 38 * @run main/timeout=2000 CancelledFutureLoops
39 39 * @summary Checks for responsiveness of futures to cancellation.
40 40 * Runs under the assumption that ITERS computations require more than
41 41 * TIMEOUT msecs to complete.
42 42 */
43 43
44 44 import java.util.concurrent.*;
45 45 import java.util.concurrent.locks.*;
46 46 import java.util.*;
47 47
48 48 public final class CancelledFutureLoops {
49 49 static final ExecutorService pool = Executors.newCachedThreadPool();
50 50 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
51 51 static boolean print = false;
52 52 static final int ITERS = 1000000;
53 53 static final long TIMEOUT = 100;
54 54
55 55 public static void main(String[] args) throws Exception {
56 56 int maxThreads = 5;
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
57 57 if (args.length > 0)
58 58 maxThreads = Integer.parseInt(args[0]);
59 59
60 60 print = true;
61 61
62 62 for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
63 63 System.out.print("Threads: " + i);
64 64 try {
65 65 new FutureLoop(i).test();
66 66 }
67 - catch(BrokenBarrierException bb) {
67 + catch (BrokenBarrierException bb) {
68 68 // OK; ignore
69 69 }
70 - catch(ExecutionException ee) {
70 + catch (ExecutionException ee) {
71 71 // OK; ignore
72 72 }
73 73 Thread.sleep(TIMEOUT);
74 74 }
75 75 pool.shutdown();
76 76 if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
77 77 throw new Error();
78 78 }
79 79
80 80 static final class FutureLoop implements Callable {
81 81 private int v = rng.next();
82 82 private final ReentrantLock lock = new ReentrantLock();
83 83 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
84 84 private final CyclicBarrier barrier;
85 85 private final int nthreads;
86 86 FutureLoop(int nthreads) {
87 87 this.nthreads = nthreads;
88 88 barrier = new CyclicBarrier(nthreads+1, timer);
89 89 }
90 90
91 91 final void test() throws Exception {
92 92 Future[] futures = new Future[nthreads];
93 93 for (int i = 0; i < nthreads; ++i)
94 94 futures[i] = pool.submit(this);
95 95
96 96 barrier.await();
97 97 Thread.sleep(TIMEOUT);
98 98 boolean tooLate = false;
99 99 for (int i = 1; i < nthreads; ++i) {
100 100 if (!futures[i].cancel(true))
101 101 tooLate = true;
102 102 // Unbunch some of the cancels
103 103 if ( (i & 3) == 0)
104 104 Thread.sleep(1 + rng.next() % 10);
105 105 }
106 106
107 107 Object f0 = futures[0].get();
108 108 if (!tooLate) {
109 109 for (int i = 1; i < nthreads; ++i) {
110 110 if (!futures[i].isDone() || !futures[i].isCancelled())
111 111 throw new Error("Only one thread should complete");
112 112 }
113 113 }
114 114 else
115 115 System.out.print("(cancelled too late) ");
116 116
117 117 long endTime = System.nanoTime();
118 118 long time = endTime - timer.startTime;
119 119 if (print) {
120 120 double secs = (double)(time) / 1000000000.0;
121 121 System.out.println("\t " + secs + "s run time");
122 122 }
123 123
124 124 }
125 125
126 126 public final Object call() throws Exception {
127 127 barrier.await();
128 128 int sum = v;
129 129 int x = 0;
130 130 int n = ITERS;
131 131 while (n-- > 0) {
132 132 lock.lockInterruptibly();
133 133 try {
134 134 v = x = LoopHelpers.compute1(v);
135 135 }
136 136 finally {
137 137 lock.unlock();
138 138 }
139 139 sum += LoopHelpers.compute2(LoopHelpers.compute2(x));
140 140 }
141 141 return new Integer(sum);
142 142 }
143 143 }
144 144
145 145 }
↓ open down ↓ |
65 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX