9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
13 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
– |
import java.math.BigInteger; |
14 |
|
import java.security.*; |
15 |
|
|
16 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
45 |
|
*/ |
46 |
|
public void testExecuteRunnable() throws Exception { |
47 |
|
ExecutorService e = new DirectExecutorService(); |
48 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
49 |
< |
assertFalse(task.done); |
50 |
< |
Future<?> future = e.submit(task); |
51 |
< |
future.get(); |
52 |
< |
assertTrue(task.done); |
48 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
49 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
50 |
> |
public void realRun() { |
51 |
> |
done.set(true); |
52 |
> |
}}); |
53 |
> |
assertNull(future.get()); |
54 |
> |
assertNull(future.get(0, MILLISECONDS)); |
55 |
> |
assertTrue(done.get()); |
56 |
> |
assertTrue(future.isDone()); |
57 |
> |
assertFalse(future.isCancelled()); |
58 |
|
} |
59 |
|
|
60 |
|
/** |
212 |
|
new ArrayBlockingQueue<Runnable>(10)); |
213 |
|
|
214 |
|
Callable c = new Callable() { |
215 |
< |
public Object call() { return 5/0; }}; |
215 |
> |
public Object call() { throw new ArithmeticException(); }}; |
216 |
|
|
217 |
|
try { |
218 |
|
p.submit(c).get(); |
256 |
|
*/ |
257 |
|
public void testInvokeAny3() throws Exception { |
258 |
|
ExecutorService e = new DirectExecutorService(); |
259 |
< |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
260 |
< |
l.add(new Callable<Integer>() { |
261 |
< |
public Integer call() { return 5/0; }}); |
259 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
260 |
> |
l.add(new Callable<Long>() { |
261 |
> |
public Long call() { throw new ArithmeticException(); }}); |
262 |
|
l.add(null); |
263 |
|
try { |
264 |
|
e.invokeAny(l); |
434 |
|
*/ |
435 |
|
public void testTimedInvokeAny3() throws Exception { |
436 |
|
ExecutorService e = new DirectExecutorService(); |
437 |
< |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
438 |
< |
l.add(new Callable<Integer>() { |
439 |
< |
public Integer call() { return 5/0; }}); |
437 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
438 |
> |
l.add(new Callable<Long>() { |
439 |
> |
public Long call() { throw new ArithmeticException(); }}); |
440 |
|
l.add(null); |
441 |
|
try { |
442 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
593 |
|
l.add(new StringTask()); |
594 |
|
List<Future<String>> futures = |
595 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
596 |
< |
assertEquals(3, futures.size()); |
597 |
< |
Iterator<Future<String>> it = futures.iterator(); |
598 |
< |
Future<String> f1 = it.next(); |
599 |
< |
Future<String> f2 = it.next(); |
600 |
< |
Future<String> f3 = it.next(); |
601 |
< |
assertTrue(f1.isDone()); |
597 |
< |
assertFalse(f1.isCancelled()); |
598 |
< |
assertTrue(f2.isDone()); |
599 |
< |
assertFalse(f2.isCancelled()); |
600 |
< |
assertTrue(f3.isDone()); |
601 |
< |
assertTrue(f3.isCancelled()); |
596 |
> |
assertEquals(l.size(), futures.size()); |
597 |
> |
for (Future future : futures) |
598 |
> |
assertTrue(future.isDone()); |
599 |
> |
assertFalse(futures.get(0).isCancelled()); |
600 |
> |
assertFalse(futures.get(1).isCancelled()); |
601 |
> |
assertTrue(futures.get(2).isCancelled()); |
602 |
|
} finally { |
603 |
|
joinPool(e); |
604 |
|
} |