6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
import junit.framework.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
– |
import java.util.concurrent.atomic.*; |
12 |
– |
import junit.framework.*; |
12 |
|
import java.util.*; |
13 |
|
|
14 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
20 |
|
} |
21 |
|
|
22 |
|
static class ExtendedTPE extends ThreadPoolExecutor { |
23 |
< |
volatile boolean beforeCalled = false; |
24 |
< |
volatile boolean afterCalled = false; |
25 |
< |
volatile boolean terminatedCalled = false; |
23 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
24 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
25 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
26 |
> |
|
27 |
|
public ExtendedTPE() { |
28 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
29 |
|
} |
30 |
|
protected void beforeExecute(Thread t, Runnable r) { |
31 |
< |
beforeCalled = true; |
31 |
> |
beforeCalled.countDown(); |
32 |
|
} |
33 |
|
protected void afterExecute(Runnable r, Throwable t) { |
34 |
< |
afterCalled = true; |
34 |
> |
afterCalled.countDown(); |
35 |
|
} |
36 |
|
protected void terminated() { |
37 |
< |
terminatedCalled = true; |
37 |
> |
terminatedCalled.countDown(); |
38 |
> |
} |
39 |
> |
|
40 |
> |
public boolean beforeCalled() { |
41 |
> |
return beforeCalled.getCount() == 0; |
42 |
> |
} |
43 |
> |
public boolean afterCalled() { |
44 |
> |
return afterCalled.getCount() == 0; |
45 |
> |
} |
46 |
> |
public boolean terminatedCalled() { |
47 |
> |
return terminatedCalled.getCount() == 0; |
48 |
|
} |
49 |
|
} |
50 |
|
|
56 |
|
} |
57 |
|
} |
58 |
|
|
49 |
– |
|
59 |
|
/** |
60 |
|
* execute successfully executes a runnable |
61 |
|
*/ |
159 |
|
threadProceed.await(); |
160 |
|
threadDone.countDown(); |
161 |
|
}}); |
162 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
162 |
> |
await(threadStarted); |
163 |
|
assertEquals(0, p.getCompletedTaskCount()); |
164 |
|
threadProceed.countDown(); |
165 |
|
threadDone.await(); |
166 |
< |
delay(SHORT_DELAY_MS); |
167 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
166 |
> |
long startTime = System.nanoTime(); |
167 |
> |
while (p.getCompletedTaskCount() != 1) { |
168 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
169 |
> |
fail("timed out"); |
170 |
> |
Thread.yield(); |
171 |
> |
} |
172 |
|
} finally { |
173 |
|
joinPool(p); |
174 |
|
} |
198 |
|
joinPool(p); |
199 |
|
} |
200 |
|
|
188 |
– |
|
201 |
|
/** |
202 |
|
* getThreadFactory returns factory in constructor if not set |
203 |
|
*/ |
227 |
|
joinPool(p); |
228 |
|
} |
229 |
|
|
218 |
– |
|
230 |
|
/** |
231 |
|
* setThreadFactory(null) throws NPE |
232 |
|
*/ |
273 |
|
joinPool(p); |
274 |
|
} |
275 |
|
|
265 |
– |
|
276 |
|
/** |
277 |
|
* setRejectedExecutionHandler(null) throws NPE |
278 |
|
*/ |
290 |
|
} |
291 |
|
} |
292 |
|
|
283 |
– |
|
293 |
|
/** |
294 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
295 |
|
* multiple threads active |
400 |
|
joinPool(p); |
401 |
|
} |
402 |
|
|
394 |
– |
|
403 |
|
/** |
404 |
|
* isTerminated is false before termination, true after |
405 |
|
*/ |
595 |
|
|
596 |
|
// Exception Tests |
597 |
|
|
590 |
– |
|
598 |
|
/** |
599 |
|
* Constructor throws if corePoolSize argument is less than zero |
600 |
|
*/ |
667 |
|
} catch (NullPointerException success) {} |
668 |
|
} |
669 |
|
|
663 |
– |
|
664 |
– |
|
670 |
|
/** |
671 |
|
* Constructor throws if corePoolSize argument is less than zero |
672 |
|
*/ |
758 |
|
} catch (NullPointerException success) {} |
759 |
|
} |
760 |
|
|
756 |
– |
|
761 |
|
/** |
762 |
|
* Constructor throws if corePoolSize argument is less than zero |
763 |
|
*/ |
849 |
|
} catch (NullPointerException success) {} |
850 |
|
} |
851 |
|
|
848 |
– |
|
852 |
|
/** |
853 |
|
* Constructor throws if corePoolSize argument is less than zero |
854 |
|
*/ |
1215 |
|
} |
1216 |
|
} |
1217 |
|
|
1215 |
– |
|
1218 |
|
/** |
1219 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1220 |
|
*/ |
1236 |
|
} |
1237 |
|
} |
1238 |
|
|
1237 |
– |
|
1239 |
|
/** |
1240 |
|
* execute(null) throws NPE |
1241 |
|
*/ |
1308 |
|
joinPool(p); |
1309 |
|
} |
1310 |
|
|
1310 |
– |
|
1311 |
|
/** |
1312 |
|
* setKeepAliveTime throws IllegalArgumentException |
1313 |
|
* when given a negative value |
1333 |
|
public void testTerminated() { |
1334 |
|
ExtendedTPE p = new ExtendedTPE(); |
1335 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1336 |
< |
assertTrue(p.terminatedCalled); |
1336 |
> |
assertTrue(p.terminatedCalled()); |
1337 |
|
joinPool(p); |
1338 |
|
} |
1339 |
|
|
1343 |
|
public void testBeforeAfter() throws InterruptedException { |
1344 |
|
ExtendedTPE p = new ExtendedTPE(); |
1345 |
|
try { |
1346 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1347 |
< |
p.execute(r); |
1348 |
< |
delay(SHORT_DELAY_MS); |
1349 |
< |
assertTrue(r.done); |
1350 |
< |
assertTrue(p.beforeCalled); |
1351 |
< |
assertTrue(p.afterCalled); |
1346 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1347 |
> |
final CheckedRunnable task = new CheckedRunnable() { |
1348 |
> |
public void realRun() { |
1349 |
> |
done.countDown(); |
1350 |
> |
}}; |
1351 |
> |
p.execute(task); |
1352 |
> |
await(p.afterCalled); |
1353 |
> |
assertEquals(0, done.getCount()); |
1354 |
> |
assertTrue(p.afterCalled()); |
1355 |
> |
assertTrue(p.beforeCalled()); |
1356 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1357 |
|
} finally { |
1358 |
|
joinPool(p); |
1410 |
|
} |
1411 |
|
} |
1412 |
|
|
1409 |
– |
|
1413 |
|
/** |
1414 |
|
* invokeAny(null) throws NPE |
1415 |
|
*/ |
1603 |
|
} |
1604 |
|
} |
1605 |
|
|
1603 |
– |
|
1604 |
– |
|
1606 |
|
/** |
1607 |
|
* timed invokeAny(null) throws NPE |
1608 |
|
*/ |
1849 |
|
l.add(new StringTask()); |
1850 |
|
List<Future<String>> futures = |
1851 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1852 |
< |
assertEquals(3, futures.size()); |
1853 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1854 |
< |
Future<String> f1 = it.next(); |
1855 |
< |
Future<String> f2 = it.next(); |
1856 |
< |
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()); |
1852 |
> |
assertEquals(l.size(), futures.size()); |
1853 |
> |
for (Future future : futures) |
1854 |
> |
assertTrue(future.isDone()); |
1855 |
> |
assertFalse(futures.get(0).isCancelled()); |
1856 |
> |
assertTrue(futures.get(1).isCancelled()); |
1857 |
|
} finally { |
1858 |
|
joinPool(e); |
1859 |
|
} |
1899 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1900 |
|
*/ |
1901 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1902 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1903 |
|
final ThreadPoolExecutor p = |
1904 |
|
new ThreadPoolExecutor(2, 10, |
1905 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1905 |
> |
coreThreadTimeOut, MILLISECONDS, |
1906 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1907 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1908 |
|
try { |
1909 |
|
p.allowCoreThreadTimeOut(true); |
1910 |
|
p.execute(new CheckedRunnable() { |
1911 |
< |
public void realRun() throws InterruptedException { |
1911 |
> |
public void realRun() { |
1912 |
|
threadStarted.countDown(); |
1913 |
|
assertEquals(1, p.getPoolSize()); |
1914 |
|
}}); |
1915 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1916 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1917 |
< |
if (p.getPoolSize() == 0) |
1918 |
< |
break; |
1919 |
< |
delay(10); |
1920 |
< |
} |
1915 |
> |
await(threadStarted); |
1916 |
> |
delay(coreThreadTimeOut); |
1917 |
> |
long startTime = System.nanoTime(); |
1918 |
> |
while (p.getPoolSize() > 0 |
1919 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1920 |
> |
Thread.yield(); |
1921 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1922 |
|
assertEquals(0, p.getPoolSize()); |
1923 |
|
} finally { |
1924 |
|
joinPool(p); |
1929 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1930 |
|
*/ |
1931 |
|
public void testAllowCoreThreadTimeOut_false() 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 { |
1942 |
|
threadStarted.countDown(); |
1943 |
|
assertTrue(p.getPoolSize() >= 1); |
1944 |
|
}}); |
1945 |
< |
delay(SMALL_DELAY_MS); |
1945 |
> |
delay(2 * coreThreadTimeOut); |
1946 |
|
assertTrue(p.getPoolSize() >= 1); |
1947 |
|
} finally { |
1948 |
|
joinPool(p); |
1976 |
|
// enough time to run all tasks |
1977 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
1978 |
|
} finally { |
1979 |
< |
p.shutdown(); |
1979 |
> |
joinPool(p); |
1980 |
|
} |
1981 |
|
} |
1982 |
|
|