64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
+ |
static class MyError extends Error {} |
68 |
+ |
|
69 |
|
// to test handlers |
70 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
71 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
72 |
< |
protected void onStart() { super.onStart(); throw new Error(); } |
72 |
> |
protected void onStart() { super.onStart(); throw new MyError(); } |
73 |
|
} |
74 |
|
|
75 |
|
static class FailingThreadFactory |
213 |
|
* getPoolSize returns number of started workers. |
214 |
|
*/ |
215 |
|
public void testGetPoolSize() { |
216 |
< |
ForkJoinPool p = new ForkJoinPool(1); |
216 |
> |
final CountDownLatch taskStarted = new CountDownLatch(1); |
217 |
> |
final CountDownLatch done = new CountDownLatch(1); |
218 |
> |
final ForkJoinPool p = new ForkJoinPool(1); |
219 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
220 |
|
assertEquals(0, p.getActiveThreadCount()); |
221 |
< |
Future<String> future = p.submit(new StringTask()); |
221 |
> |
final Runnable task = new CheckedRunnable() { |
222 |
> |
public void realRun() throws InterruptedException { |
223 |
> |
taskStarted.countDown(); |
224 |
> |
assertEquals(1, p.getPoolSize()); |
225 |
> |
assertEquals(1, p.getActiveThreadCount()); |
226 |
> |
done.await(); |
227 |
> |
}}; |
228 |
> |
Future<?> future = p.submit(task); |
229 |
> |
await(taskStarted); |
230 |
|
assertEquals(1, p.getPoolSize()); |
231 |
+ |
assertEquals(1, p.getActiveThreadCount()); |
232 |
+ |
done.countDown(); |
233 |
|
} |
234 |
+ |
assertEquals(0, p.getPoolSize()); |
235 |
+ |
assertEquals(0, p.getActiveThreadCount()); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
272 |
|
*/ |
273 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
274 |
|
final CountDownLatch uehInvoked = new CountDownLatch(1); |
275 |
< |
final Thread.UncaughtExceptionHandler eh = |
275 |
> |
final Thread.UncaughtExceptionHandler ueh = |
276 |
|
new Thread.UncaughtExceptionHandler() { |
277 |
|
public void uncaughtException(Thread t, Throwable e) { |
278 |
+ |
threadAssertTrue(e instanceof MyError); |
279 |
+ |
threadAssertTrue(t instanceof FailingFJWSubclass); |
280 |
|
uehInvoked.countDown(); |
281 |
|
}}; |
282 |
|
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
283 |
< |
eh, false); |
284 |
< |
try { |
285 |
< |
assertSame(eh, p.getUncaughtExceptionHandler()); |
283 |
> |
ueh, false); |
284 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
285 |
> |
assertSame(ueh, p.getUncaughtExceptionHandler()); |
286 |
|
try { |
287 |
|
p.execute(new FibTask(8)); |
288 |
< |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
289 |
< |
} catch (RejectedExecutionException ok) { |
288 |
> |
await(uehInvoked); |
289 |
> |
} finally { |
290 |
> |
p.shutdownNow(); // failure might have prevented processing task |
291 |
|
} |
273 |
– |
} finally { |
274 |
– |
p.shutdownNow(); // failure might have prevented processing task |
275 |
– |
joinPool(p); |
292 |
|
} |
293 |
|
} |
294 |
|
|
319 |
|
|
320 |
|
assertTrue(p.isQuiescent()); |
321 |
|
assertFalse(p.getAsyncMode()); |
306 |
– |
assertEquals(0, p.getActiveThreadCount()); |
322 |
|
assertEquals(0, p.getQueuedTaskCount()); |
323 |
|
assertEquals(0, p.getQueuedSubmissionCount()); |
324 |
|
assertFalse(p.hasQueuedSubmissions()); |
325 |
+ |
while (p.getActiveThreadCount() != 0 |
326 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
327 |
+ |
Thread.yield(); |
328 |
|
assertFalse(p.isShutdown()); |
329 |
|
assertFalse(p.isTerminating()); |
330 |
|
assertFalse(p.isTerminated()); |
331 |
|
assertTrue(f.isDone()); |
332 |
|
assertEquals(6765, (int) f.get()); |
333 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
334 |
|
} |
335 |
|
} |
336 |
|
|
567 |
|
public void testInterruptedSubmit() throws InterruptedException { |
568 |
|
final CountDownLatch submitted = new CountDownLatch(1); |
569 |
|
final CountDownLatch quittingTime = new CountDownLatch(1); |
551 |
– |
final ExecutorService p = new ForkJoinPool(1); |
570 |
|
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
571 |
|
public Void realCall() throws InterruptedException { |
572 |
< |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
572 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
573 |
|
return null; |
574 |
|
}}; |
575 |
< |
try { |
575 |
> |
final ExecutorService p = new ForkJoinPool(1); |
576 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
577 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
578 |
|
public void realRun() throws Exception { |
579 |
|
Future<Void> future = p.submit(awaiter); |
581 |
|
future.get(); |
582 |
|
}}); |
583 |
|
t.start(); |
584 |
< |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
584 |
> |
await(submitted); |
585 |
|
t.interrupt(); |
586 |
< |
t.join(); |
568 |
< |
} finally { |
569 |
< |
quittingTime.countDown(); |
570 |
< |
joinPool(p); |
586 |
> |
awaitTermination(t); |
587 |
|
} |
588 |
|
} |
589 |
|
|
838 |
|
public void testTimedInvokeAny4() throws Throwable { |
839 |
|
ExecutorService e = new ForkJoinPool(1); |
840 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
841 |
+ |
long startTime = System.nanoTime(); |
842 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
843 |
|
l.add(new NPETask()); |
844 |
|
try { |
845 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
845 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
846 |
|
shouldThrow(); |
847 |
|
} catch (ExecutionException success) { |
848 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
849 |
|
} |
850 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
851 |
|
} |
852 |
|
} |
853 |
|
|
857 |
|
public void testTimedInvokeAny5() throws Throwable { |
858 |
|
ExecutorService e = new ForkJoinPool(1); |
859 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
860 |
+ |
long startTime = System.nanoTime(); |
861 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
862 |
|
l.add(new StringTask()); |
863 |
|
l.add(new StringTask()); |
864 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
864 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
865 |
|
assertSame(TEST_STRING, result); |
866 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
867 |
|
} |
868 |
|
} |
869 |
|
|