1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
53 |
|
} catch (NullPointerException success) {} |
54 |
|
} |
55 |
|
|
57 |
– |
|
56 |
|
/** |
57 |
|
* A new SingleThreadExecutor can execute runnables |
58 |
|
*/ |
99 |
|
} |
100 |
|
} |
101 |
|
|
104 |
– |
|
102 |
|
/** |
103 |
|
* A new newFixedThreadPool can execute runnables |
104 |
|
*/ |
141 |
|
} catch (IllegalArgumentException success) {} |
142 |
|
} |
143 |
|
|
147 |
– |
|
144 |
|
/** |
145 |
|
* An unconfigurable newFixedThreadPool can execute runnables |
146 |
|
*/ |
172 |
|
} catch (NullPointerException success) {} |
173 |
|
} |
174 |
|
|
179 |
– |
|
175 |
|
/** |
176 |
|
* a newSingleThreadScheduledExecutor successfully runs delayed task |
177 |
|
*/ |
178 |
|
public void testNewSingleThreadScheduledExecutor() throws Exception { |
179 |
< |
TrackedCallable callable = new TrackedCallable(); |
180 |
< |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
181 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
182 |
< |
assertFalse(callable.done); |
183 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
184 |
< |
assertTrue(callable.done); |
185 |
< |
assertEquals(Boolean.TRUE, f.get()); |
186 |
< |
joinPool(p1); |
179 |
> |
ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); |
180 |
> |
try { |
181 |
> |
final CountDownLatch done = new CountDownLatch(1); |
182 |
> |
final Runnable task = new CheckedRunnable() { |
183 |
> |
public void realRun() { |
184 |
> |
done.countDown(); |
185 |
> |
}}; |
186 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
187 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
188 |
> |
assertFalse(f.isDone()); |
189 |
> |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
190 |
> |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
191 |
> |
assertSame(Boolean.TRUE, f.get()); |
192 |
> |
assertTrue(f.isDone()); |
193 |
> |
} finally { |
194 |
> |
joinPool(p); |
195 |
> |
} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
199 |
|
* a newScheduledThreadPool successfully runs delayed task |
200 |
|
*/ |
201 |
|
public void testnewScheduledThreadPool() throws Exception { |
202 |
< |
TrackedCallable callable = new TrackedCallable(); |
203 |
< |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
204 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
205 |
< |
assertFalse(callable.done); |
206 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
207 |
< |
assertTrue(callable.done); |
208 |
< |
assertEquals(Boolean.TRUE, f.get()); |
209 |
< |
joinPool(p1); |
202 |
> |
ScheduledExecutorService p = Executors.newScheduledThreadPool(2); |
203 |
> |
try { |
204 |
> |
final CountDownLatch done = new CountDownLatch(1); |
205 |
> |
final Runnable task = new CheckedRunnable() { |
206 |
> |
public void realRun() { |
207 |
> |
done.countDown(); |
208 |
> |
}}; |
209 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
210 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
211 |
> |
assertFalse(f.isDone()); |
212 |
> |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
213 |
> |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
214 |
> |
assertSame(Boolean.TRUE, f.get()); |
215 |
> |
assertTrue(f.isDone()); |
216 |
> |
} finally { |
217 |
> |
joinPool(p); |
218 |
> |
} |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
|
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
223 |
|
*/ |
224 |
|
public void testunconfigurableScheduledExecutorService() throws Exception { |
225 |
< |
TrackedCallable callable = new TrackedCallable(); |
226 |
< |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
227 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
228 |
< |
assertFalse(callable.done); |
229 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
230 |
< |
assertTrue(callable.done); |
231 |
< |
assertEquals(Boolean.TRUE, f.get()); |
232 |
< |
joinPool(p1); |
225 |
> |
ScheduledExecutorService p = |
226 |
> |
Executors.unconfigurableScheduledExecutorService |
227 |
> |
(Executors.newScheduledThreadPool(2)); |
228 |
> |
try { |
229 |
> |
final CountDownLatch done = new CountDownLatch(1); |
230 |
> |
final Runnable task = new CheckedRunnable() { |
231 |
> |
public void realRun() { |
232 |
> |
done.countDown(); |
233 |
> |
}}; |
234 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
235 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
236 |
> |
assertFalse(f.isDone()); |
237 |
> |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
238 |
> |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
239 |
> |
assertSame(Boolean.TRUE, f.get()); |
240 |
> |
assertTrue(f.isDone()); |
241 |
> |
} finally { |
242 |
> |
joinPool(p); |
243 |
> |
} |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* Future.get on submitted tasks will time out if they compute too long. |
247 |
> |
* Future.get on submitted tasks will time out if they compute too long. |
248 |
|
*/ |
249 |
|
public void testTimedCallable() throws Exception { |
250 |
< |
final Runnable sleeper = |
251 |
< |
new RunnableShouldThrow(InterruptedException.class) { |
252 |
< |
public void realRun() throws InterruptedException { |
253 |
< |
Thread.sleep(LONG_DELAY_MS); |
254 |
< |
}}; |
255 |
< |
for (ExecutorService executor : |
256 |
< |
new ExecutorService[] { |
257 |
< |
Executors.newSingleThreadExecutor(), |
258 |
< |
Executors.newCachedThreadPool(), |
259 |
< |
Executors.newFixedThreadPool(2), |
260 |
< |
Executors.newScheduledThreadPool(2), |
261 |
< |
}) { |
262 |
< |
try { |
263 |
< |
Future future = executor.submit(sleeper); |
264 |
< |
try { |
265 |
< |
future.get(SHORT_DELAY_MS, MILLISECONDS); |
266 |
< |
shouldThrow(); |
267 |
< |
} catch (TimeoutException success) { |
268 |
< |
} finally { |
269 |
< |
future.cancel(true); |
246 |
< |
} |
247 |
< |
} |
248 |
< |
finally { |
249 |
< |
joinPool(executor); |
250 |
< |
} |
250 |
> |
final ExecutorService[] executors = { |
251 |
> |
Executors.newSingleThreadExecutor(), |
252 |
> |
Executors.newCachedThreadPool(), |
253 |
> |
Executors.newFixedThreadPool(2), |
254 |
> |
Executors.newScheduledThreadPool(2), |
255 |
> |
}; |
256 |
> |
|
257 |
> |
final Runnable sleeper = new CheckedInterruptedRunnable() { |
258 |
> |
public void realRun() throws InterruptedException { |
259 |
> |
delay(LONG_DELAY_MS); |
260 |
> |
}}; |
261 |
> |
|
262 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
263 |
> |
for (final ExecutorService executor : executors) { |
264 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
265 |
> |
public void realRun() { |
266 |
> |
long startTime = System.nanoTime(); |
267 |
> |
Future future = executor.submit(sleeper); |
268 |
> |
assertFutureTimesOut(future); |
269 |
> |
}})); |
270 |
|
} |
271 |
+ |
for (Thread thread : threads) |
272 |
+ |
awaitTermination(thread); |
273 |
+ |
for (ExecutorService executor : executors) |
274 |
+ |
joinPool(executor); |
275 |
|
} |
276 |
|
|
254 |
– |
|
277 |
|
/** |
278 |
|
* ThreadPoolExecutor using defaultThreadFactory has |
279 |
|
* specified group, priority, daemon status, and name |
280 |
|
*/ |
281 |
|
public void testDefaultThreadFactory() throws Exception { |
282 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
283 |
< |
Runnable r = new Runnable() { |
284 |
< |
public void run() { |
285 |
< |
try { |
286 |
< |
Thread current = Thread.currentThread(); |
287 |
< |
threadAssertTrue(!current.isDaemon()); |
288 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
289 |
< |
ThreadGroup g = current.getThreadGroup(); |
290 |
< |
SecurityManager s = System.getSecurityManager(); |
291 |
< |
if (s != null) |
292 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
293 |
< |
else |
294 |
< |
threadAssertTrue(g == egroup); |
295 |
< |
String name = current.getName(); |
296 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
297 |
< |
} catch (SecurityException ok) { |
298 |
< |
// Also pass if not allowed to change setting |
277 |
< |
} |
283 |
> |
Runnable r = new CheckedRunnable() { |
284 |
> |
public void realRun() { |
285 |
> |
try { |
286 |
> |
Thread current = Thread.currentThread(); |
287 |
> |
assertTrue(!current.isDaemon()); |
288 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
289 |
> |
ThreadGroup g = current.getThreadGroup(); |
290 |
> |
SecurityManager s = System.getSecurityManager(); |
291 |
> |
if (s != null) |
292 |
> |
assertTrue(g == s.getThreadGroup()); |
293 |
> |
else |
294 |
> |
assertTrue(g == egroup); |
295 |
> |
String name = current.getName(); |
296 |
> |
assertTrue(name.endsWith("thread-1")); |
297 |
> |
} catch (SecurityException ok) { |
298 |
> |
// Also pass if not allowed to change setting |
299 |
|
} |
300 |
< |
}; |
300 |
> |
}}; |
301 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
302 |
|
|
303 |
|
e.execute(r); |
307 |
|
} |
308 |
|
|
309 |
|
try { |
310 |
< |
Thread.sleep(SHORT_DELAY_MS); |
310 |
> |
delay(SHORT_DELAY_MS); |
311 |
|
} finally { |
312 |
|
joinPool(e); |
313 |
|
} |
337 |
|
assertTrue(g == egroup); |
338 |
|
String name = current.getName(); |
339 |
|
assertTrue(name.endsWith("thread-1")); |
340 |
< |
assertTrue(thisccl == current.getContextClassLoader()); |
341 |
< |
assertTrue(thisacc.equals(AccessController.getContext())); |
340 |
> |
assertSame(thisccl, current.getContextClassLoader()); |
341 |
> |
assertEquals(thisacc, AccessController.getContext()); |
342 |
|
}}; |
343 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
344 |
|
e.execute(r); |
345 |
|
e.shutdown(); |
346 |
< |
Thread.sleep(SHORT_DELAY_MS); |
346 |
> |
delay(SHORT_DELAY_MS); |
347 |
|
joinPool(e); |
348 |
|
}}; |
349 |
|
|
381 |
|
} |
382 |
|
} |
383 |
|
|
363 |
– |
|
384 |
|
/** |
385 |
|
* Without class loader permissions, creating |
386 |
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
420 |
|
* Without permissions, calling privilegedCallable throws ACE |
421 |
|
*/ |
422 |
|
public void testprivilegedCallableWithNoPrivs() throws Exception { |
423 |
+ |
// Avoid classloader-related SecurityExceptions in swingui.TestRunner |
424 |
+ |
Executors.privilegedCallable(new CheckCCL()); |
425 |
+ |
|
426 |
|
Runnable r = new CheckedRunnable() { |
427 |
|
public void realRun() throws Exception { |
428 |
|
if (System.getSecurityManager() == null) |
497 |
|
Executors.privilegedCallable(new CheckCCL()).call(); |
498 |
|
}}; |
499 |
|
|
500 |
< |
runWithPermissions(r, |
500 |
> |
runWithPermissions(r, |
501 |
|
new RuntimePermission("getClassLoader"), |
502 |
|
new RuntimePermission("setContextClassLoader")); |
503 |
|
} |
536 |
|
assertSame(one, c.call()); |
537 |
|
} |
538 |
|
|
516 |
– |
|
539 |
|
/** |
540 |
|
* callable(null Runnable) throws NPE |
541 |
|
*/ |
576 |
|
} catch (NullPointerException success) {} |
577 |
|
} |
578 |
|
|
557 |
– |
|
579 |
|
} |