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 |
|
|
60 |
– |
static class MyHandler implements Thread.UncaughtExceptionHandler { |
61 |
– |
volatile int catches = 0; |
62 |
– |
public void uncaughtException(Thread t, Throwable e) { |
63 |
– |
++catches; |
64 |
– |
} |
65 |
– |
} |
66 |
– |
|
61 |
|
static class MyError extends Error {} |
62 |
|
|
63 |
|
// to test handlers |
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 |
|
/** |
313 |
|
|
314 |
|
assertTrue(p.isQuiescent()); |
315 |
|
assertFalse(p.getAsyncMode()); |
308 |
– |
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 |
|
|
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 |
|
|