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 |
|
|
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 |
|
} |
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); |
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(); |
1839 |
< |
assertTrue(f1.isDone()); |
1840 |
< |
assertTrue(f2.isDone()); |
1841 |
< |
assertTrue(f3.isDone()); |
1842 |
< |
assertFalse(f1.isCancelled()); |
1843 |
< |
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 |
|
} |