7 |
|
*/ |
8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
|
import java.util.ArrayList; |
13 |
+ |
import java.util.HashSet; |
14 |
|
import java.util.List; |
15 |
|
import java.util.concurrent.BlockingQueue; |
16 |
|
import java.util.concurrent.Callable; |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
< |
* shutdownNow returns a list containing tasks that were not run |
656 |
> |
* shutdownNow returns a list containing tasks that were not run, |
657 |
> |
* and those tasks are drained from the queue |
658 |
|
*/ |
659 |
< |
public void testShutdownNow() { |
659 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
660 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
661 |
< |
for (int i = 0; i < 5; i++) |
662 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
663 |
< |
LONG_DELAY_MS, MILLISECONDS); |
661 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
662 |
> |
for (int i = 0; i < 3; i++) { |
663 |
> |
Runnable r = new NoOpRunnable(); |
664 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
665 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
666 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
667 |
> |
} |
668 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
669 |
> |
final List<Runnable> queuedTasks; |
670 |
|
try { |
671 |
< |
List<Runnable> l = p.shutdownNow(); |
663 |
< |
assertTrue(p.isShutdown()); |
664 |
< |
assertEquals(5, l.size()); |
671 |
> |
queuedTasks = p.shutdownNow(); |
672 |
|
} catch (SecurityException ok) { |
673 |
< |
// Allowed in case test doesn't have privs |
667 |
< |
} finally { |
668 |
< |
joinPool(p); |
673 |
> |
return; // Allowed in case test doesn't have privs |
674 |
|
} |
675 |
+ |
assertTrue(p.isShutdown()); |
676 |
+ |
assertTrue(p.getQueue().isEmpty()); |
677 |
+ |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
678 |
+ |
assertEquals(tasks.size(), queuedTasks.size()); |
679 |
+ |
for (ScheduledFuture task : tasks) { |
680 |
+ |
assertFalse(task.isDone()); |
681 |
+ |
assertFalse(task.isCancelled()); |
682 |
+ |
} |
683 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
684 |
+ |
assertTrue(p.isTerminated()); |
685 |
|
} |
686 |
|
|
687 |
|
/** |