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 |
< |
delay(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 |
|
|
394 |
– |
|
404 |
|
/** |
405 |
|
* isTerminated is false before termination, true after |
406 |
|
*/ |
573 |
|
} |
574 |
|
|
575 |
|
/** |
576 |
< |
* shutDownNow returns a list containing tasks that were not run |
576 |
> |
* shutdownNow returns a list containing tasks that were not run |
577 |
|
*/ |
578 |
< |
public void testShutDownNow() { |
578 |
> |
public void testShutdownNow() { |
579 |
|
final ThreadPoolExecutor p = |
580 |
|
new ThreadPoolExecutor(1, 1, |
581 |
|
LONG_DELAY_MS, MILLISECONDS, |
596 |
|
|
597 |
|
// Exception Tests |
598 |
|
|
590 |
– |
|
599 |
|
/** |
600 |
|
* Constructor throws if corePoolSize argument is less than zero |
601 |
|
*/ |
668 |
|
} catch (NullPointerException success) {} |
669 |
|
} |
670 |
|
|
663 |
– |
|
664 |
– |
|
671 |
|
/** |
672 |
|
* Constructor throws if corePoolSize argument is less than zero |
673 |
|
*/ |
759 |
|
} catch (NullPointerException success) {} |
760 |
|
} |
761 |
|
|
756 |
– |
|
762 |
|
/** |
763 |
|
* Constructor throws if corePoolSize argument is less than zero |
764 |
|
*/ |
850 |
|
} catch (NullPointerException success) {} |
851 |
|
} |
852 |
|
|
848 |
– |
|
853 |
|
/** |
854 |
|
* Constructor throws if corePoolSize argument is less than zero |
855 |
|
*/ |
1216 |
|
} |
1217 |
|
} |
1218 |
|
|
1215 |
– |
|
1219 |
|
/** |
1220 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1221 |
|
*/ |
1237 |
|
} |
1238 |
|
} |
1239 |
|
|
1237 |
– |
|
1240 |
|
/** |
1241 |
|
* execute(null) throws NPE |
1242 |
|
*/ |
1309 |
|
joinPool(p); |
1310 |
|
} |
1311 |
|
|
1310 |
– |
|
1312 |
|
/** |
1313 |
|
* setKeepAliveTime throws IllegalArgumentException |
1314 |
|
* when given a negative value |
1334 |
|
public void testTerminated() { |
1335 |
|
ExtendedTPE p = new ExtendedTPE(); |
1336 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1337 |
< |
assertTrue(p.terminatedCalled); |
1337 |
> |
assertTrue(p.terminatedCalled()); |
1338 |
|
joinPool(p); |
1339 |
|
} |
1340 |
|
|
1344 |
|
public void testBeforeAfter() throws InterruptedException { |
1345 |
|
ExtendedTPE p = new ExtendedTPE(); |
1346 |
|
try { |
1347 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1348 |
< |
p.execute(r); |
1349 |
< |
delay(SHORT_DELAY_MS); |
1350 |
< |
assertTrue(r.done); |
1351 |
< |
assertTrue(p.beforeCalled); |
1352 |
< |
assertTrue(p.afterCalled); |
1347 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1348 |
> |
final CheckedRunnable task = new CheckedRunnable() { |
1349 |
> |
public void realRun() { |
1350 |
> |
done.countDown(); |
1351 |
> |
}}; |
1352 |
> |
p.execute(task); |
1353 |
> |
await(p.afterCalled); |
1354 |
> |
assertEquals(0, done.getCount()); |
1355 |
> |
assertTrue(p.afterCalled()); |
1356 |
> |
assertTrue(p.beforeCalled()); |
1357 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1358 |
|
} finally { |
1359 |
|
joinPool(p); |
1411 |
|
} |
1412 |
|
} |
1413 |
|
|
1409 |
– |
|
1414 |
|
/** |
1415 |
|
* invokeAny(null) throws NPE |
1416 |
|
*/ |
1604 |
|
} |
1605 |
|
} |
1606 |
|
|
1603 |
– |
|
1604 |
– |
|
1607 |
|
/** |
1608 |
|
* timed invokeAny(null) throws NPE |
1609 |
|
*/ |
1850 |
|
l.add(new StringTask()); |
1851 |
|
List<Future<String>> futures = |
1852 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1853 |
< |
assertEquals(3, futures.size()); |
1854 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1855 |
< |
Future<String> f1 = it.next(); |
1856 |
< |
Future<String> f2 = it.next(); |
1857 |
< |
Future<String> f3 = it.next(); |
1856 |
< |
assertTrue(f1.isDone()); |
1857 |
< |
assertTrue(f2.isDone()); |
1858 |
< |
assertTrue(f3.isDone()); |
1859 |
< |
assertFalse(f1.isCancelled()); |
1860 |
< |
assertTrue(f2.isCancelled()); |
1853 |
> |
assertEquals(l.size(), futures.size()); |
1854 |
> |
for (Future future : futures) |
1855 |
> |
assertTrue(future.isDone()); |
1856 |
> |
assertFalse(futures.get(0).isCancelled()); |
1857 |
> |
assertTrue(futures.get(1).isCancelled()); |
1858 |
|
} finally { |
1859 |
|
joinPool(e); |
1860 |
|
} |
1900 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1901 |
|
*/ |
1902 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1903 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1904 |
|
final ThreadPoolExecutor p = |
1905 |
|
new ThreadPoolExecutor(2, 10, |
1906 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1906 |
> |
coreThreadTimeOut, MILLISECONDS, |
1907 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1908 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1909 |
|
try { |
1910 |
|
p.allowCoreThreadTimeOut(true); |
1911 |
|
p.execute(new CheckedRunnable() { |
1912 |
< |
public void realRun() throws InterruptedException { |
1912 |
> |
public void realRun() { |
1913 |
|
threadStarted.countDown(); |
1914 |
|
assertEquals(1, p.getPoolSize()); |
1915 |
|
}}); |
1916 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1917 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1918 |
< |
if (p.getPoolSize() == 0) |
1919 |
< |
break; |
1920 |
< |
delay(10); |
1921 |
< |
} |
1916 |
> |
await(threadStarted); |
1917 |
> |
delay(coreThreadTimeOut); |
1918 |
> |
long startTime = System.nanoTime(); |
1919 |
> |
while (p.getPoolSize() > 0 |
1920 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1921 |
> |
Thread.yield(); |
1922 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1923 |
|
assertEquals(0, p.getPoolSize()); |
1924 |
|
} finally { |
1925 |
|
joinPool(p); |
1930 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1931 |
|
*/ |
1932 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1933 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1934 |
|
final ThreadPoolExecutor p = |
1935 |
|
new ThreadPoolExecutor(2, 10, |
1936 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1936 |
> |
coreThreadTimeOut, MILLISECONDS, |
1937 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1938 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1939 |
|
try { |
1943 |
|
threadStarted.countDown(); |
1944 |
|
assertTrue(p.getPoolSize() >= 1); |
1945 |
|
}}); |
1946 |
< |
delay(SMALL_DELAY_MS); |
1946 |
> |
delay(2 * coreThreadTimeOut); |
1947 |
|
assertTrue(p.getPoolSize() >= 1); |
1948 |
|
} finally { |
1949 |
|
joinPool(p); |
1977 |
|
// enough time to run all tasks |
1978 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
1979 |
|
} finally { |
1980 |
< |
p.shutdown(); |
1980 |
> |
joinPool(p); |
1981 |
|
} |
1982 |
|
} |
1983 |
|
|