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 |
+ |
import junit.framework.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
< |
import java.util.concurrent.atomic.*; |
12 |
< |
import junit.framework.*; |
12 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
13 |
|
import java.util.*; |
14 |
|
|
15 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
21 |
|
} |
22 |
|
|
23 |
|
static class ExtendedTPE extends ThreadPoolExecutor { |
24 |
< |
volatile boolean beforeCalled = false; |
25 |
< |
volatile boolean afterCalled = false; |
26 |
< |
volatile boolean terminatedCalled = false; |
24 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
25 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
26 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
27 |
> |
|
28 |
|
public ExtendedTPE() { |
29 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
30 |
|
} |
31 |
|
protected void beforeExecute(Thread t, Runnable r) { |
32 |
< |
beforeCalled = true; |
32 |
> |
beforeCalled.countDown(); |
33 |
|
} |
34 |
|
protected void afterExecute(Runnable r, Throwable t) { |
35 |
< |
afterCalled = true; |
35 |
> |
afterCalled.countDown(); |
36 |
|
} |
37 |
|
protected void terminated() { |
38 |
< |
terminatedCalled = true; |
38 |
> |
terminatedCalled.countDown(); |
39 |
> |
} |
40 |
> |
|
41 |
> |
public boolean beforeCalled() { |
42 |
> |
return beforeCalled.getCount() == 0; |
43 |
> |
} |
44 |
> |
public boolean afterCalled() { |
45 |
> |
return afterCalled.getCount() == 0; |
46 |
> |
} |
47 |
> |
public boolean terminatedCalled() { |
48 |
> |
return terminatedCalled.getCount() == 0; |
49 |
|
} |
50 |
|
} |
51 |
|
|
57 |
|
} |
58 |
|
} |
59 |
|
|
49 |
– |
|
60 |
|
/** |
61 |
|
* execute successfully executes a runnable |
62 |
|
*/ |
160 |
|
threadProceed.await(); |
161 |
|
threadDone.countDown(); |
162 |
|
}}); |
163 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
163 |
> |
await(threadStarted); |
164 |
|
assertEquals(0, p.getCompletedTaskCount()); |
165 |
|
threadProceed.countDown(); |
166 |
|
threadDone.await(); |
167 |
< |
Thread.sleep(SHORT_DELAY_MS); |
168 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
167 |
> |
long startTime = System.nanoTime(); |
168 |
> |
while (p.getCompletedTaskCount() != 1) { |
169 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
170 |
> |
fail("timed out"); |
171 |
> |
Thread.yield(); |
172 |
> |
} |
173 |
|
} finally { |
174 |
|
joinPool(p); |
175 |
|
} |
199 |
|
joinPool(p); |
200 |
|
} |
201 |
|
|
188 |
– |
|
202 |
|
/** |
203 |
|
* getThreadFactory returns factory in constructor if not set |
204 |
|
*/ |
228 |
|
joinPool(p); |
229 |
|
} |
230 |
|
|
218 |
– |
|
231 |
|
/** |
232 |
|
* setThreadFactory(null) throws NPE |
233 |
|
*/ |
274 |
|
joinPool(p); |
275 |
|
} |
276 |
|
|
265 |
– |
|
277 |
|
/** |
278 |
|
* setRejectedExecutionHandler(null) throws NPE |
279 |
|
*/ |
291 |
|
} |
292 |
|
} |
293 |
|
|
283 |
– |
|
294 |
|
/** |
295 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
296 |
|
* multiple threads active |
388 |
|
} |
389 |
|
|
390 |
|
/** |
391 |
< |
* isShutDown is false before shutdown, true after |
391 |
> |
* isShutdown is false before shutdown, true after |
392 |
|
*/ |
393 |
|
public void testIsShutdown() { |
394 |
|
final ThreadPoolExecutor p = |
401 |
|
joinPool(p); |
402 |
|
} |
403 |
|
|
404 |
+ |
/** |
405 |
+ |
* awaitTermination on a non-shutdown pool times out |
406 |
+ |
*/ |
407 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
408 |
+ |
final ThreadPoolExecutor p = |
409 |
+ |
new ThreadPoolExecutor(1, 1, |
410 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
411 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
412 |
+ |
assertFalse(p.isTerminated()); |
413 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
414 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
415 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
416 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
417 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
418 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
419 |
+ |
long timeoutNanos = 999999L; |
420 |
+ |
long startTime = System.nanoTime(); |
421 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
422 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
423 |
+ |
assertFalse(p.isTerminated()); |
424 |
+ |
startTime = System.nanoTime(); |
425 |
+ |
long timeoutMillis = timeoutMillis(); |
426 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
427 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
428 |
+ |
assertFalse(p.isTerminated()); |
429 |
+ |
p.shutdown(); |
430 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
431 |
+ |
assertTrue(p.isTerminated()); |
432 |
+ |
} |
433 |
|
|
434 |
|
/** |
435 |
|
* isTerminated is false before termination, true after |
445 |
|
try { |
446 |
|
p.execute(new CheckedRunnable() { |
447 |
|
public void realRun() throws InterruptedException { |
409 |
– |
threadStarted.countDown(); |
448 |
|
assertFalse(p.isTerminated()); |
449 |
+ |
threadStarted.countDown(); |
450 |
|
done.await(); |
451 |
|
}}); |
452 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
453 |
+ |
assertFalse(p.isTerminating()); |
454 |
|
done.countDown(); |
455 |
|
} finally { |
456 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
473 |
|
assertFalse(p.isTerminating()); |
474 |
|
p.execute(new CheckedRunnable() { |
475 |
|
public void realRun() throws InterruptedException { |
436 |
– |
threadStarted.countDown(); |
476 |
|
assertFalse(p.isTerminating()); |
477 |
+ |
threadStarted.countDown(); |
478 |
|
done.await(); |
479 |
|
}}); |
480 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
603 |
|
} |
604 |
|
|
605 |
|
/** |
606 |
< |
* shutDownNow returns a list containing tasks that were not run |
606 |
> |
* shutdownNow returns a list containing tasks that were not run |
607 |
|
*/ |
608 |
< |
public void testShutDownNow() { |
608 |
> |
public void testShutdownNow() { |
609 |
|
final ThreadPoolExecutor p = |
610 |
|
new ThreadPoolExecutor(1, 1, |
611 |
|
LONG_DELAY_MS, MILLISECONDS, |
626 |
|
|
627 |
|
// Exception Tests |
628 |
|
|
589 |
– |
|
629 |
|
/** |
630 |
|
* Constructor throws if corePoolSize argument is less than zero |
631 |
|
*/ |
698 |
|
} catch (NullPointerException success) {} |
699 |
|
} |
700 |
|
|
662 |
– |
|
663 |
– |
|
701 |
|
/** |
702 |
|
* Constructor throws if corePoolSize argument is less than zero |
703 |
|
*/ |
789 |
|
} catch (NullPointerException success) {} |
790 |
|
} |
791 |
|
|
755 |
– |
|
792 |
|
/** |
793 |
|
* Constructor throws if corePoolSize argument is less than zero |
794 |
|
*/ |
880 |
|
} catch (NullPointerException success) {} |
881 |
|
} |
882 |
|
|
847 |
– |
|
883 |
|
/** |
884 |
|
* Constructor throws if corePoolSize argument is less than zero |
885 |
|
*/ |
1246 |
|
} |
1247 |
|
} |
1248 |
|
|
1214 |
– |
|
1249 |
|
/** |
1250 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1251 |
|
*/ |
1267 |
|
} |
1268 |
|
} |
1269 |
|
|
1236 |
– |
|
1270 |
|
/** |
1271 |
|
* execute(null) throws NPE |
1272 |
|
*/ |
1339 |
|
joinPool(p); |
1340 |
|
} |
1341 |
|
|
1309 |
– |
|
1342 |
|
/** |
1343 |
|
* setKeepAliveTime throws IllegalArgumentException |
1344 |
|
* when given a negative value |
1364 |
|
public void testTerminated() { |
1365 |
|
ExtendedTPE p = new ExtendedTPE(); |
1366 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1367 |
< |
assertTrue(p.terminatedCalled); |
1367 |
> |
assertTrue(p.terminatedCalled()); |
1368 |
|
joinPool(p); |
1369 |
|
} |
1370 |
|
|
1374 |
|
public void testBeforeAfter() throws InterruptedException { |
1375 |
|
ExtendedTPE p = new ExtendedTPE(); |
1376 |
|
try { |
1377 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1378 |
< |
p.execute(r); |
1379 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1380 |
< |
assertTrue(r.done); |
1381 |
< |
assertTrue(p.beforeCalled); |
1382 |
< |
assertTrue(p.afterCalled); |
1377 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1378 |
> |
p.execute(new CheckedRunnable() { |
1379 |
> |
public void realRun() { |
1380 |
> |
done.countDown(); |
1381 |
> |
}}); |
1382 |
> |
await(p.afterCalled); |
1383 |
> |
assertEquals(0, done.getCount()); |
1384 |
> |
assertTrue(p.afterCalled()); |
1385 |
> |
assertTrue(p.beforeCalled()); |
1386 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1387 |
|
} finally { |
1388 |
|
joinPool(p); |
1440 |
|
} |
1441 |
|
} |
1442 |
|
|
1408 |
– |
|
1443 |
|
/** |
1444 |
|
* invokeAny(null) throws NPE |
1445 |
|
*/ |
1633 |
|
} |
1634 |
|
} |
1635 |
|
|
1602 |
– |
|
1603 |
– |
|
1636 |
|
/** |
1637 |
|
* timed invokeAny(null) throws NPE |
1638 |
|
*/ |
1879 |
|
l.add(new StringTask()); |
1880 |
|
List<Future<String>> futures = |
1881 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1882 |
< |
assertEquals(3, futures.size()); |
1883 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1884 |
< |
Future<String> f1 = it.next(); |
1885 |
< |
Future<String> f2 = it.next(); |
1886 |
< |
Future<String> f3 = it.next(); |
1855 |
< |
assertTrue(f1.isDone()); |
1856 |
< |
assertTrue(f2.isDone()); |
1857 |
< |
assertTrue(f3.isDone()); |
1858 |
< |
assertFalse(f1.isCancelled()); |
1859 |
< |
assertTrue(f2.isCancelled()); |
1882 |
> |
assertEquals(l.size(), futures.size()); |
1883 |
> |
for (Future future : futures) |
1884 |
> |
assertTrue(future.isDone()); |
1885 |
> |
assertFalse(futures.get(0).isCancelled()); |
1886 |
> |
assertTrue(futures.get(1).isCancelled()); |
1887 |
|
} finally { |
1888 |
|
joinPool(e); |
1889 |
|
} |
1929 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1930 |
|
*/ |
1931 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1932 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1933 |
|
final ThreadPoolExecutor p = |
1934 |
|
new ThreadPoolExecutor(2, 10, |
1935 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1935 |
> |
coreThreadTimeOut, MILLISECONDS, |
1936 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1937 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1938 |
|
try { |
1939 |
|
p.allowCoreThreadTimeOut(true); |
1940 |
|
p.execute(new CheckedRunnable() { |
1941 |
< |
public void realRun() throws InterruptedException { |
1941 |
> |
public void realRun() { |
1942 |
|
threadStarted.countDown(); |
1943 |
|
assertEquals(1, p.getPoolSize()); |
1944 |
|
}}); |
1945 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1946 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1947 |
< |
if (p.getPoolSize() == 0) |
1948 |
< |
break; |
1949 |
< |
Thread.sleep(10); |
1950 |
< |
} |
1945 |
> |
await(threadStarted); |
1946 |
> |
delay(coreThreadTimeOut); |
1947 |
> |
long startTime = System.nanoTime(); |
1948 |
> |
while (p.getPoolSize() > 0 |
1949 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1950 |
> |
Thread.yield(); |
1951 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1952 |
|
assertEquals(0, p.getPoolSize()); |
1953 |
|
} finally { |
1954 |
|
joinPool(p); |
1959 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1960 |
|
*/ |
1961 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1962 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1963 |
|
final ThreadPoolExecutor p = |
1964 |
|
new ThreadPoolExecutor(2, 10, |
1965 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1965 |
> |
coreThreadTimeOut, MILLISECONDS, |
1966 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1967 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1968 |
|
try { |
1972 |
|
threadStarted.countDown(); |
1973 |
|
assertTrue(p.getPoolSize() >= 1); |
1974 |
|
}}); |
1975 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1975 |
> |
delay(2 * coreThreadTimeOut); |
1976 |
|
assertTrue(p.getPoolSize() >= 1); |
1977 |
|
} finally { |
1978 |
|
joinPool(p); |
2006 |
|
// enough time to run all tasks |
2007 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2008 |
|
} finally { |
2009 |
< |
p.shutdown(); |
2009 |
> |
joinPool(p); |
2010 |
|
} |
2011 |
|
} |
2012 |
|
|