30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
|
import java.util.concurrent.TimeoutException; |
32 |
|
import java.util.concurrent.TimeUnit; |
33 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
34 |
|
import java.util.concurrent.locks.Condition; |
35 |
|
import java.util.concurrent.locks.ReentrantLock; |
36 |
|
|
703 |
|
* shutdownNow returns a list containing tasks that were not run, |
704 |
|
* and those tasks are drained from the queue |
705 |
|
*/ |
706 |
< |
public void testShutdownNow() { |
707 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
708 |
< |
List l; |
709 |
< |
try { |
710 |
< |
for (int i = 0; i < 5; i++) |
711 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
712 |
< |
} |
713 |
< |
finally { |
706 |
> |
public void testShutdownNow() throws InterruptedException { |
707 |
> |
final int poolSize = 2; |
708 |
> |
final int count = 5; |
709 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
710 |
> |
ThreadPoolExecutor p = |
711 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
712 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
713 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
714 |
> |
CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { |
715 |
> |
threadsStarted.countDown(); |
716 |
|
try { |
717 |
< |
l = p.shutdownNow(); |
718 |
< |
} catch (SecurityException ok) { return; } |
717 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
718 |
> |
} catch (InterruptedException success) {} |
719 |
> |
ran.getAndIncrement(); |
720 |
> |
}}; |
721 |
> |
for (int i = 0; i < count; i++) |
722 |
> |
p.execute(waiter); |
723 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
724 |
> |
final List<Runnable> queuedTasks; |
725 |
> |
try { |
726 |
> |
queuedTasks = p.shutdownNow(); |
727 |
> |
} catch (SecurityException ok) { |
728 |
> |
return; // Allowed in case test doesn't have privs |
729 |
|
} |
730 |
|
assertTrue(p.isShutdown()); |
731 |
|
assertTrue(p.getQueue().isEmpty()); |
732 |
< |
assertTrue(l.size() <= 4); |
732 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
733 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
734 |
> |
assertTrue(p.isTerminated()); |
735 |
> |
assertEquals(poolSize, ran.get()); |
736 |
|
} |
737 |
|
|
738 |
|
// Exception Tests |