Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java
+++ new/test/java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java
1 1 /*
2 2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 /*
25 25 * @test
26 26 * @bug 6450200
27 27 * @summary Test proper handling of pool state changes
28 28 * @run main/othervm ConfigChanges
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
29 29 * @author Martin Buchholz
30 30 */
31 31
32 32 import java.security.*;
33 33 import java.util.*;
34 34 import java.util.concurrent.*;
35 35 import java.util.concurrent.atomic.*;
36 36 import static java.util.concurrent.TimeUnit.*;
37 37
38 38 public class ConfigChanges {
39 - final static ThreadGroup tg = new ThreadGroup("pool");
39 + static final ThreadGroup tg = new ThreadGroup("pool");
40 40
41 - final static Random rnd = new Random();
41 + static final Random rnd = new Random();
42 42
43 43 static void report(ThreadPoolExecutor tpe) {
44 44 try {
45 45 System.out.printf(
46 46 "active=%d submitted=%d completed=%d queued=%d sizes=%d/%d/%d%n",
47 47 tg.activeCount(),
48 48 tpe.getTaskCount(),
49 49 tpe.getCompletedTaskCount(),
50 50 tpe.getQueue().size(),
51 51 tpe.getPoolSize(),
52 52 tpe.getCorePoolSize(),
53 53 tpe.getMaximumPoolSize());
54 54 } catch (Throwable t) { unexpected(t); }
55 55 }
56 56
57 57 static void report(String label, ThreadPoolExecutor tpe) {
58 58 System.out.printf("%10s ", label);
59 59 report(tpe);
60 60 }
61 61
62 62 static class PermissiveSecurityManger extends SecurityManager {
63 63 public void checkPermission(Permission p) { /* bien sur, Monsieur */ }
64 64 }
65 65
66 66 static void checkShutdown(final ExecutorService es) {
67 67 final Runnable nop = new Runnable() {public void run() {}};
68 68 try {
69 69 if (new Random().nextBoolean()) {
70 70 check(es.isShutdown());
71 71 if (es instanceof ThreadPoolExecutor)
72 72 check(((ThreadPoolExecutor) es).isTerminating()
73 73 || es.isTerminated());
74 74 THROWS(RejectedExecutionException.class,
75 75 new Fun() {void f() {es.execute(nop);}});
76 76 }
77 77 } catch (Throwable t) { unexpected(t); }
78 78 }
79 79
80 80 static void checkTerminated(final ThreadPoolExecutor tpe) {
81 81 try {
82 82 checkShutdown(tpe);
83 83 check(tpe.getQueue().isEmpty());
84 84 check(tpe.isTerminated());
85 85 check(! tpe.isTerminating());
86 86 equal(tpe.getActiveCount(), 0);
87 87 equal(tpe.getPoolSize(), 0);
88 88 equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
89 89 check(tpe.awaitTermination(0, SECONDS));
90 90 } catch (Throwable t) { unexpected(t); }
91 91 }
92 92
93 93 static Runnable waiter(final CyclicBarrier barrier) {
94 94 return new Runnable() { public void run() {
95 95 try { barrier.await(); barrier.await(); }
96 96 catch (Throwable t) { unexpected(t); }}};
97 97 }
98 98
99 99 static volatile Runnable runnableDuJour;
100 100
101 101 private static void realMain(String[] args) throws Throwable {
102 102 if (rnd.nextBoolean())
103 103 System.setSecurityManager(new PermissiveSecurityManger());
104 104
105 105 final boolean prestart = rnd.nextBoolean();
106 106
107 107 final Thread.UncaughtExceptionHandler handler
108 108 = new Thread.UncaughtExceptionHandler() {
109 109 public void uncaughtException(Thread t, Throwable e) {
110 110 check(! Thread.currentThread().isInterrupted());
111 111 unexpected(e);
112 112 }};
113 113
114 114 final int n = 3;
115 115 final ThreadPoolExecutor tpe
116 116 = new ThreadPoolExecutor(n, 3*n,
117 117 3L, MINUTES,
118 118 new ArrayBlockingQueue<Runnable>(3*n));
119 119 tpe.setThreadFactory(new ThreadFactory() {
120 120 public Thread newThread(Runnable r) {
121 121 Thread t = new Thread(tg, r);
122 122 t.setUncaughtExceptionHandler(handler);
123 123 return t;
124 124 }});
125 125
126 126 if (prestart) {
127 127 tpe.prestartAllCoreThreads();
128 128 equal(tg.activeCount(), n);
129 129 equal(tg.activeCount(), tpe.getCorePoolSize());
130 130 }
131 131
132 132 final Runnable runRunnableDuJour =
133 133 new Runnable() { public void run() {
134 134 // Delay choice of action till last possible moment.
135 135 runnableDuJour.run(); }};
136 136 final CyclicBarrier pumpedUp = new CyclicBarrier(3*n + 1);
137 137 runnableDuJour = waiter(pumpedUp);
138 138
139 139 if (prestart) {
140 140 for (int i = 0; i < 1*n; i++)
141 141 tpe.execute(runRunnableDuJour);
142 142 // Wait for prestarted threads to dequeue their initial tasks.
143 143 while (! tpe.getQueue().isEmpty())
144 144 Thread.sleep(10);
145 145 for (int i = 0; i < 5*n; i++)
146 146 tpe.execute(runRunnableDuJour);
147 147 } else {
148 148 for (int i = 0; i < 6*n; i++)
149 149 tpe.execute(runRunnableDuJour);
150 150 }
151 151
152 152 //report("submitted", tpe);
153 153 pumpedUp.await();
154 154 equal(tg.activeCount(), 3*n);
155 155 equal(tg.activeCount(), tpe.getMaximumPoolSize());
156 156 equal(tpe.getCorePoolSize(), n);
157 157 //report("pumped up", tpe);
158 158 equal(tpe.getMaximumPoolSize(), 3*n);
159 159 tpe.setMaximumPoolSize(4*n);
160 160 equal(tpe.getMaximumPoolSize(), 4*n);
161 161 //report("pumped up2", tpe);
162 162 final CyclicBarrier pumpedUp2 = new CyclicBarrier(n + 1);
163 163 runnableDuJour = waiter(pumpedUp2);
164 164 for (int i = 0; i < 1*n; i++)
165 165 tpe.execute(runRunnableDuJour);
166 166 pumpedUp2.await();
167 167 equal(tg.activeCount(), 4*n);
168 168 equal(tg.activeCount(), tpe.getMaximumPoolSize());
169 169 equal(tpe.getCompletedTaskCount(), 0L);
170 170 //report("pumped up2", tpe);
171 171 runnableDuJour = new Runnable() { public void run() {}};
172 172
173 173 tpe.setMaximumPoolSize(2*n);
174 174 //report("after set", tpe);
175 175
176 176 pumpedUp2.await();
177 177 pumpedUp.await();
178 178
179 179 // while (tg.activeCount() != n &&
180 180 // tg.activeCount() != n)
181 181 // Thread.sleep(10);
182 182 // equal(tg.activeCount(), n);
183 183 // equal(tg.activeCount(), tpe.getCorePoolSize());
184 184
185 185 while (tg.activeCount() != 2*n &&
186 186 tg.activeCount() != 2*n)
187 187 Thread.sleep(10);
188 188 equal(tg.activeCount(), 2*n);
189 189 equal(tg.activeCount(), tpe.getMaximumPoolSize());
190 190
191 191
192 192 //report("draining", tpe);
193 193 while (tpe.getCompletedTaskCount() < 7*n &&
194 194 tpe.getCompletedTaskCount() < 7*n)
195 195 Thread.sleep(10);
196 196
197 197 //equal(tg.activeCount(), n);
198 198 //equal(tg.activeCount(), tpe.getCorePoolSize());
199 199 equal(tg.activeCount(), 2*n);
200 200 equal(tg.activeCount(), tpe.getMaximumPoolSize());
201 201
202 202 equal(tpe.getTaskCount(), 7L*n);
203 203 equal(tpe.getCompletedTaskCount(), 7L*n);
204 204
205 205 equal(tpe.getKeepAliveTime(MINUTES), 3L);
206 206 tpe.setKeepAliveTime(7L, MILLISECONDS);
207 207 equal(tpe.getKeepAliveTime(MILLISECONDS), 7L);
208 208 while (tg.activeCount() > n &&
209 209 tg.activeCount() > n)
210 210 Thread.sleep(10);
211 211 equal(tg.activeCount(), n);
212 212
213 213 //report("idle", tpe);
214 214 check(! tpe.allowsCoreThreadTimeOut());
215 215 tpe.allowCoreThreadTimeOut(true);
216 216 check(tpe.allowsCoreThreadTimeOut());
217 217 while (tg.activeCount() > 0 &&
218 218 tg.activeCount() > 0)
219 219 Thread.sleep(10);
220 220 equal(tg.activeCount(), 0);
221 221
222 222 //report("idle", tpe);
223 223
224 224 tpe.shutdown();
225 225 checkShutdown(tpe);
226 226 check(tpe.awaitTermination(3L, MINUTES));
227 227 checkTerminated(tpe);
228 228 }
229 229
230 230 //--------------------- Infrastructure ---------------------------
231 231 static volatile int passed = 0, failed = 0;
232 232 static void pass() {passed++;}
233 233 static void fail() {failed++; Thread.dumpStack();}
↓ open down ↓ |
182 lines elided |
↑ open up ↑ |
234 234 static void fail(String msg) {System.out.println(msg); fail();}
235 235 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
236 236 static void check(boolean cond) {if (cond) pass(); else fail();}
237 237 static void equal(Object x, Object y) {
238 238 if (x == null ? y == null : x.equals(y)) pass();
239 239 else fail(x + " not equal to " + y);}
240 240 public static void main(String[] args) throws Throwable {
241 241 try {realMain(args);} catch (Throwable t) {unexpected(t);}
242 242 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
243 243 if (failed > 0) throw new AssertionError("Some tests failed");}
244 - private static abstract class Fun {abstract void f() throws Throwable;}
244 + private abstract static class Fun {abstract void f() throws Throwable;}
245 245 static void THROWS(Class<? extends Throwable> k, Fun... fs) {
246 246 for (Fun f : fs)
247 247 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
248 248 catch (Throwable t) {
249 249 if (k.isAssignableFrom(t.getClass())) pass();
250 250 else unexpected(t);}}
251 251 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX