14 |
|
import java.util.List; |
15 |
|
import java.util.concurrent.ArrayBlockingQueue; |
16 |
|
import java.util.concurrent.BlockingQueue; |
17 |
– |
import java.util.concurrent.CancellationException; |
17 |
|
import java.util.concurrent.Callable; |
18 |
+ |
import java.util.concurrent.CancellationException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.ExecutionException; |
21 |
|
import java.util.concurrent.Executors; |
29 |
|
import java.util.concurrent.ThreadFactory; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
|
import java.util.concurrent.TimeUnit; |
32 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
33 |
|
|
34 |
|
import junit.framework.Test; |
35 |
|
import junit.framework.TestSuite; |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
< |
* shutdownNow returns a list containing tasks that were not run |
628 |
> |
* shutdownNow returns a list containing tasks that were not run, |
629 |
> |
* and those tasks are drained from the queue |
630 |
|
*/ |
631 |
< |
public void testShutdownNow() { |
631 |
> |
public void testShutdownNow() throws InterruptedException { |
632 |
> |
final int poolSize = 2; |
633 |
> |
final int count = 5; |
634 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
635 |
|
final ThreadPoolExecutor p = |
636 |
< |
new ThreadPoolExecutor(1, 1, |
636 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
637 |
|
LONG_DELAY_MS, MILLISECONDS, |
638 |
|
new ArrayBlockingQueue<Runnable>(10)); |
639 |
< |
List l; |
640 |
< |
try { |
641 |
< |
for (int i = 0; i < 5; i++) |
637 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
638 |
< |
} |
639 |
< |
finally { |
639 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
640 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
641 |
> |
threadsStarted.countDown(); |
642 |
|
try { |
643 |
< |
l = p.shutdownNow(); |
644 |
< |
} catch (SecurityException ok) { return; } |
643 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
644 |
> |
} catch (InterruptedException success) {} |
645 |
> |
ran.getAndIncrement(); |
646 |
> |
}}; |
647 |
> |
for (int i = 0; i < count; i++) |
648 |
> |
p.execute(waiter); |
649 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
650 |
> |
assertEquals(poolSize, p.getActiveCount()); |
651 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
652 |
> |
final List<Runnable> queuedTasks; |
653 |
> |
try { |
654 |
> |
queuedTasks = p.shutdownNow(); |
655 |
> |
} catch (SecurityException ok) { |
656 |
> |
return; // Allowed in case test doesn't have privs |
657 |
|
} |
658 |
|
assertTrue(p.isShutdown()); |
659 |
< |
assertTrue(l.size() <= 4); |
659 |
> |
assertTrue(p.getQueue().isEmpty()); |
660 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
661 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
662 |
> |
assertTrue(p.isTerminated()); |
663 |
> |
assertEquals(poolSize, ran.get()); |
664 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
665 |
|
} |
666 |
|
|
667 |
|
// Exception Tests |
1914 |
|
try { |
1915 |
|
for (long timeout = timeoutMillis();;) { |
1916 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1917 |
< |
tasks.add(new StringTask()); |
1917 |
> |
tasks.add(new StringTask("0")); |
1918 |
|
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1919 |
< |
tasks.add(new StringTask()); |
1919 |
> |
tasks.add(new StringTask("2")); |
1920 |
|
long startTime = System.nanoTime(); |
1921 |
|
List<Future<String>> futures = |
1922 |
|
e.invokeAll(tasks, timeout, MILLISECONDS); |
1926 |
|
assertTrue(future.isDone()); |
1927 |
|
assertTrue(futures.get(1).isCancelled()); |
1928 |
|
try { |
1929 |
< |
assertEquals(TEST_STRING, futures.get(0).get()); |
1930 |
< |
assertEquals(TEST_STRING, futures.get(2).get()); |
1929 |
> |
assertEquals("0", futures.get(0).get()); |
1930 |
> |
assertEquals("2", futures.get(2).get()); |
1931 |
|
break; |
1932 |
|
} catch (CancellationException retryWithLongerTimeout) { |
1933 |
|
timeout *= 2; |
2062 |
|
} |
2063 |
|
} |
2064 |
|
|
2065 |
+ |
/** |
2066 |
+ |
* get(cancelled task) throws CancellationException |
2067 |
+ |
*/ |
2068 |
+ |
public void testGet_cancelled() throws Exception { |
2069 |
+ |
final ExecutorService e = |
2070 |
+ |
new ThreadPoolExecutor(1, 1, |
2071 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
2072 |
+ |
new LinkedBlockingQueue<Runnable>()); |
2073 |
+ |
try { |
2074 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2075 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2076 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
2077 |
+ |
for (int i = 0; i < 2; i++) { |
2078 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
2079 |
+ |
throws Throwable { |
2080 |
+ |
blockerStarted.countDown(); |
2081 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2082 |
+ |
}}; |
2083 |
+ |
futures.add(e.submit(r)); |
2084 |
+ |
} |
2085 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2086 |
+ |
for (Future<?> future : futures) future.cancel(false); |
2087 |
+ |
for (Future<?> future : futures) { |
2088 |
+ |
try { |
2089 |
+ |
future.get(); |
2090 |
+ |
shouldThrow(); |
2091 |
+ |
} catch (CancellationException success) {} |
2092 |
+ |
try { |
2093 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2094 |
+ |
shouldThrow(); |
2095 |
+ |
} catch (CancellationException success) {} |
2096 |
+ |
assertTrue(future.isCancelled()); |
2097 |
+ |
assertTrue(future.isDone()); |
2098 |
+ |
} |
2099 |
+ |
done.countDown(); |
2100 |
+ |
} finally { |
2101 |
+ |
joinPool(e); |
2102 |
+ |
} |
2103 |
+ |
} |
2104 |
+ |
|
2105 |
|
} |