24 |
|
import java.util.concurrent.RecursiveTask; |
25 |
|
import java.util.concurrent.RejectedExecutionException; |
26 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
27 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
28 |
|
import java.util.concurrent.locks.ReentrantLock; |
29 |
|
|
30 |
|
import junit.framework.AssertionFailedError; |
58 |
|
|
59 |
|
// Some classes to test extension and factory methods |
60 |
|
|
61 |
< |
static class MyHandler implements Thread.UncaughtExceptionHandler { |
61 |
< |
volatile int catches = 0; |
62 |
< |
public void uncaughtException(Thread t, Throwable e) { |
63 |
< |
++catches; |
64 |
< |
} |
65 |
< |
} |
61 |
> |
static class MyError extends Error {} |
62 |
|
|
63 |
|
// to test handlers |
64 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
65 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
66 |
< |
protected void onStart() { super.onStart(); throw new Error(); } |
66 |
> |
protected void onStart() { super.onStart(); throw new MyError(); } |
67 |
|
} |
68 |
|
|
69 |
|
static class FailingThreadFactory |
70 |
|
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
71 |
< |
volatile int calls = 0; |
71 |
> |
final AtomicInteger calls = new AtomicInteger(0); |
72 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
73 |
< |
if (++calls > 1) return null; |
73 |
> |
if (calls.incrementAndGet() > 1) return null; |
74 |
|
return new FailingFJWSubclass(p); |
75 |
|
} |
76 |
|
} |
207 |
|
* getPoolSize returns number of started workers. |
208 |
|
*/ |
209 |
|
public void testGetPoolSize() { |
210 |
< |
ForkJoinPool p = new ForkJoinPool(1); |
210 |
> |
final CountDownLatch taskStarted = new CountDownLatch(1); |
211 |
> |
final CountDownLatch done = new CountDownLatch(1); |
212 |
> |
final ForkJoinPool p = new ForkJoinPool(1); |
213 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
214 |
|
assertEquals(0, p.getActiveThreadCount()); |
215 |
< |
Future<String> future = p.submit(new StringTask()); |
215 |
> |
final Runnable task = new CheckedRunnable() { |
216 |
> |
public void realRun() throws InterruptedException { |
217 |
> |
taskStarted.countDown(); |
218 |
> |
assertEquals(1, p.getPoolSize()); |
219 |
> |
assertEquals(1, p.getActiveThreadCount()); |
220 |
> |
done.await(); |
221 |
> |
}}; |
222 |
> |
Future<?> future = p.submit(task); |
223 |
> |
await(taskStarted); |
224 |
|
assertEquals(1, p.getPoolSize()); |
225 |
+ |
assertEquals(1, p.getActiveThreadCount()); |
226 |
+ |
done.countDown(); |
227 |
|
} |
228 |
+ |
assertEquals(0, p.getPoolSize()); |
229 |
+ |
assertEquals(0, p.getActiveThreadCount()); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
266 |
|
*/ |
267 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
268 |
|
final CountDownLatch uehInvoked = new CountDownLatch(1); |
269 |
< |
final Thread.UncaughtExceptionHandler eh = |
269 |
> |
final Thread.UncaughtExceptionHandler ueh = |
270 |
|
new Thread.UncaughtExceptionHandler() { |
271 |
|
public void uncaughtException(Thread t, Throwable e) { |
272 |
+ |
threadAssertTrue(e instanceof MyError); |
273 |
+ |
threadAssertTrue(t instanceof FailingFJWSubclass); |
274 |
|
uehInvoked.countDown(); |
275 |
|
}}; |
276 |
|
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
277 |
< |
eh, false); |
278 |
< |
try { |
279 |
< |
assertSame(eh, p.getUncaughtExceptionHandler()); |
277 |
> |
ueh, false); |
278 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
279 |
> |
assertSame(ueh, p.getUncaughtExceptionHandler()); |
280 |
|
try { |
281 |
|
p.execute(new FibTask(8)); |
282 |
< |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
283 |
< |
} catch (RejectedExecutionException ok) { |
282 |
> |
await(uehInvoked); |
283 |
> |
} finally { |
284 |
> |
p.shutdownNow(); // failure might have prevented processing task |
285 |
|
} |
273 |
– |
} finally { |
274 |
– |
p.shutdownNow(); // failure might have prevented processing task |
275 |
– |
joinPool(p); |
286 |
|
} |
287 |
|
} |
288 |
|
|
313 |
|
|
314 |
|
assertTrue(p.isQuiescent()); |
315 |
|
assertFalse(p.getAsyncMode()); |
306 |
– |
assertEquals(0, p.getActiveThreadCount()); |
316 |
|
assertEquals(0, p.getQueuedTaskCount()); |
317 |
|
assertEquals(0, p.getQueuedSubmissionCount()); |
318 |
|
assertFalse(p.hasQueuedSubmissions()); |
319 |
+ |
while (p.getActiveThreadCount() != 0 |
320 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
321 |
+ |
Thread.yield(); |
322 |
|
assertFalse(p.isShutdown()); |
323 |
|
assertFalse(p.isTerminating()); |
324 |
|
assertFalse(p.isTerminated()); |
325 |
|
assertTrue(f.isDone()); |
326 |
|
assertEquals(6765, (int) f.get()); |
327 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
328 |
|
} |
329 |
|
} |
330 |
|
|
561 |
|
public void testInterruptedSubmit() throws InterruptedException { |
562 |
|
final CountDownLatch submitted = new CountDownLatch(1); |
563 |
|
final CountDownLatch quittingTime = new CountDownLatch(1); |
551 |
– |
final ExecutorService p = new ForkJoinPool(1); |
564 |
|
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
565 |
|
public Void realCall() throws InterruptedException { |
566 |
< |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
566 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
567 |
|
return null; |
568 |
|
}}; |
569 |
< |
try { |
569 |
> |
final ExecutorService p = new ForkJoinPool(1); |
570 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
571 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
572 |
|
public void realRun() throws Exception { |
573 |
|
Future<Void> future = p.submit(awaiter); |
575 |
|
future.get(); |
576 |
|
}}); |
577 |
|
t.start(); |
578 |
< |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
578 |
> |
await(submitted); |
579 |
|
t.interrupt(); |
580 |
< |
t.join(); |
568 |
< |
} finally { |
569 |
< |
quittingTime.countDown(); |
570 |
< |
joinPool(p); |
580 |
> |
awaitTermination(t); |
581 |
|
} |
582 |
|
} |
583 |
|
|
832 |
|
public void testTimedInvokeAny4() throws Throwable { |
833 |
|
ExecutorService e = new ForkJoinPool(1); |
834 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
835 |
+ |
long startTime = System.nanoTime(); |
836 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
837 |
|
l.add(new NPETask()); |
838 |
|
try { |
839 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
839 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
840 |
|
shouldThrow(); |
841 |
|
} catch (ExecutionException success) { |
842 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
843 |
|
} |
844 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
845 |
|
} |
846 |
|
} |
847 |
|
|
851 |
|
public void testTimedInvokeAny5() throws Throwable { |
852 |
|
ExecutorService e = new ForkJoinPool(1); |
853 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
854 |
+ |
long startTime = System.nanoTime(); |
855 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
856 |
|
l.add(new StringTask()); |
857 |
|
l.add(new StringTask()); |
858 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
858 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
859 |
|
assertSame(TEST_STRING, result); |
860 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
861 |
|
} |
862 |
|
} |
863 |
|
|
927 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
928 |
|
l.add(new NPETask()); |
929 |
|
List<Future<String>> futures |
930 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
930 |
> |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
931 |
|
assertEquals(1, futures.size()); |
932 |
|
try { |
933 |
|
futures.get(0).get(); |