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 |
|
|
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 |
|
} |
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 |
436 |
|
*/ |
437 |
|
public void testIsTerminated() throws InterruptedException { |
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 |
< |
delay(SHORT_DELAY_MS); |
1380 |
< |
assertTrue(r.done); |
1381 |
< |
assertTrue(p.beforeCalled); |
1382 |
< |
assertTrue(p.afterCalled); |
1377 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1378 |
> |
final CheckedRunnable task = new CheckedRunnable() { |
1379 |
> |
public void realRun() { |
1380 |
> |
done.countDown(); |
1381 |
> |
}}; |
1382 |
> |
p.execute(task); |
1383 |
> |
await(p.afterCalled); |
1384 |
> |
assertEquals(0, done.getCount()); |
1385 |
> |
assertTrue(p.afterCalled()); |
1386 |
> |
assertTrue(p.beforeCalled()); |
1387 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1388 |
|
} finally { |
1389 |
|
joinPool(p); |
1880 |
|
l.add(new StringTask()); |
1881 |
|
List<Future<String>> futures = |
1882 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1883 |
< |
assertEquals(3, futures.size()); |
1884 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1885 |
< |
Future<String> f1 = it.next(); |
1886 |
< |
Future<String> f2 = it.next(); |
1887 |
< |
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()); |
1883 |
> |
assertEquals(l.size(), futures.size()); |
1884 |
> |
for (Future future : futures) |
1885 |
> |
assertTrue(future.isDone()); |
1886 |
> |
assertFalse(futures.get(0).isCancelled()); |
1887 |
> |
assertTrue(futures.get(1).isCancelled()); |
1888 |
|
} finally { |
1889 |
|
joinPool(e); |
1890 |
|
} |
2007 |
|
// enough time to run all tasks |
2008 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2009 |
|
} finally { |
2010 |
< |
p.shutdown(); |
2010 |
> |
joinPool(p); |
2011 |
|
} |
2012 |
|
} |
2013 |
|
|