161 |
|
workQueue, threadFactory, handler); |
162 |
|
} |
163 |
|
|
164 |
< |
volatile boolean beforeCalled = false; |
165 |
< |
volatile boolean afterCalled = false; |
166 |
< |
volatile boolean terminatedCalled = false; |
164 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
165 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
166 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
167 |
> |
|
168 |
|
public CustomTPE() { |
169 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
170 |
|
} |
171 |
|
protected void beforeExecute(Thread t, Runnable r) { |
172 |
< |
beforeCalled = true; |
172 |
> |
beforeCalled.countDown(); |
173 |
|
} |
174 |
|
protected void afterExecute(Runnable r, Throwable t) { |
175 |
< |
afterCalled = true; |
175 |
> |
afterCalled.countDown(); |
176 |
|
} |
177 |
|
protected void terminated() { |
178 |
< |
terminatedCalled = true; |
178 |
> |
terminatedCalled.countDown(); |
179 |
|
} |
180 |
|
|
181 |
+ |
public boolean beforeCalled() { |
182 |
+ |
return beforeCalled.getCount() == 0; |
183 |
+ |
} |
184 |
+ |
public boolean afterCalled() { |
185 |
+ |
return afterCalled.getCount() == 0; |
186 |
+ |
} |
187 |
+ |
public boolean terminatedCalled() { |
188 |
+ |
return terminatedCalled.getCount() == 0; |
189 |
+ |
} |
190 |
|
} |
191 |
|
|
192 |
|
static class FailingThreadFactory implements ThreadFactory { |
294 |
|
threadProceed.await(); |
295 |
|
threadDone.countDown(); |
296 |
|
}}); |
297 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
297 |
> |
await(threadStarted); |
298 |
|
assertEquals(0, p.getCompletedTaskCount()); |
299 |
|
threadProceed.countDown(); |
300 |
|
threadDone.await(); |
301 |
< |
delay(SHORT_DELAY_MS); |
302 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
301 |
> |
long startTime = System.nanoTime(); |
302 |
> |
while (p.getCompletedTaskCount() != 1) { |
303 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
304 |
> |
fail("timed out"); |
305 |
> |
Thread.yield(); |
306 |
> |
} |
307 |
|
} finally { |
308 |
|
joinPool(p); |
309 |
|
} |
1237 |
|
public void testTerminated() { |
1238 |
|
CustomTPE p = new CustomTPE(); |
1239 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1240 |
< |
assertTrue(p.terminatedCalled); |
1240 |
> |
assertTrue(p.terminatedCalled()); |
1241 |
|
joinPool(p); |
1242 |
|
} |
1243 |
|
|
1247 |
|
public void testBeforeAfter() throws InterruptedException { |
1248 |
|
CustomTPE p = new CustomTPE(); |
1249 |
|
try { |
1250 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1251 |
< |
p.execute(r); |
1252 |
< |
delay(SHORT_DELAY_MS); |
1253 |
< |
assertTrue(r.done); |
1254 |
< |
assertTrue(p.beforeCalled); |
1255 |
< |
assertTrue(p.afterCalled); |
1250 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1251 |
> |
final CheckedRunnable task = new CheckedRunnable() { |
1252 |
> |
public void realRun() { |
1253 |
> |
done.countDown(); |
1254 |
> |
}}; |
1255 |
> |
p.execute(task); |
1256 |
> |
await(p.afterCalled); |
1257 |
> |
assertEquals(0, done.getCount()); |
1258 |
> |
assertTrue(p.afterCalled()); |
1259 |
> |
assertTrue(p.beforeCalled()); |
1260 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1261 |
|
} finally { |
1262 |
|
joinPool(p); |
1673 |
|
l.add(new StringTask()); |
1674 |
|
List<Future<String>> futures = |
1675 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1676 |
< |
assertEquals(3, futures.size()); |
1677 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1678 |
< |
Future<String> f1 = it.next(); |
1679 |
< |
Future<String> f2 = it.next(); |
1680 |
< |
Future<String> f3 = it.next(); |
1663 |
< |
assertTrue(f1.isDone()); |
1664 |
< |
assertTrue(f2.isDone()); |
1665 |
< |
assertTrue(f3.isDone()); |
1666 |
< |
assertFalse(f1.isCancelled()); |
1667 |
< |
assertTrue(f2.isCancelled()); |
1676 |
> |
assertEquals(l.size(), futures.size()); |
1677 |
> |
for (Future future : futures) |
1678 |
> |
assertTrue(future.isDone()); |
1679 |
> |
assertFalse(futures.get(0).isCancelled()); |
1680 |
> |
assertTrue(futures.get(1).isCancelled()); |
1681 |
|
} finally { |
1682 |
|
joinPool(e); |
1683 |
|
} |