11 |
|
import java.security.PrivilegedExceptionAction; |
12 |
|
import java.util.ArrayList; |
13 |
|
import java.util.Collection; |
14 |
+ |
import java.util.Collections; |
15 |
|
import java.util.List; |
16 |
|
import java.util.concurrent.Callable; |
17 |
|
import java.util.concurrent.CountDownLatch; |
25 |
|
import java.util.concurrent.RecursiveTask; |
26 |
|
import java.util.concurrent.RejectedExecutionException; |
27 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
28 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
29 |
|
import java.util.concurrent.locks.ReentrantLock; |
30 |
|
|
31 |
|
import junit.framework.AssertionFailedError; |
59 |
|
|
60 |
|
// Some classes to test extension and factory methods |
61 |
|
|
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 |
– |
|
62 |
|
static class MyError extends Error {} |
63 |
|
|
64 |
|
// to test handlers |
69 |
|
|
70 |
|
static class FailingThreadFactory |
71 |
|
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
72 |
< |
volatile int calls = 0; |
72 |
> |
final AtomicInteger calls = new AtomicInteger(0); |
73 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
74 |
< |
if (++calls > 1) return null; |
74 |
> |
if (calls.incrementAndGet() > 1) return null; |
75 |
|
return new FailingFJWSubclass(p); |
76 |
|
} |
77 |
|
} |
208 |
|
* getPoolSize returns number of started workers. |
209 |
|
*/ |
210 |
|
public void testGetPoolSize() { |
211 |
< |
ForkJoinPool p = new ForkJoinPool(1); |
211 |
> |
final CountDownLatch taskStarted = new CountDownLatch(1); |
212 |
> |
final CountDownLatch done = new CountDownLatch(1); |
213 |
> |
final ForkJoinPool p = new ForkJoinPool(1); |
214 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
215 |
|
assertEquals(0, p.getActiveThreadCount()); |
216 |
< |
Future<String> future = p.submit(new StringTask()); |
216 |
> |
final Runnable task = new CheckedRunnable() { |
217 |
> |
public void realRun() throws InterruptedException { |
218 |
> |
taskStarted.countDown(); |
219 |
> |
assertEquals(1, p.getPoolSize()); |
220 |
> |
assertEquals(1, p.getActiveThreadCount()); |
221 |
> |
await(done); |
222 |
> |
}}; |
223 |
> |
Future<?> future = p.submit(task); |
224 |
> |
await(taskStarted); |
225 |
|
assertEquals(1, p.getPoolSize()); |
226 |
+ |
assertEquals(1, p.getActiveThreadCount()); |
227 |
+ |
done.countDown(); |
228 |
|
} |
229 |
+ |
assertEquals(0, p.getPoolSize()); |
230 |
+ |
assertEquals(0, p.getActiveThreadCount()); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
241 |
|
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
242 |
|
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
243 |
|
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
244 |
< |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
245 |
< |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
244 |
> |
assertFalse(p.awaitTermination(randomExpiredTimeout(), |
245 |
> |
randomTimeUnit())); |
246 |
|
long timeoutNanos = 999999L; |
247 |
|
long startTime = System.nanoTime(); |
248 |
|
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
314 |
|
|
315 |
|
assertTrue(p.isQuiescent()); |
316 |
|
assertFalse(p.getAsyncMode()); |
308 |
– |
assertEquals(0, p.getActiveThreadCount()); |
317 |
|
assertEquals(0, p.getQueuedTaskCount()); |
318 |
|
assertEquals(0, p.getQueuedSubmissionCount()); |
319 |
|
assertFalse(p.hasQueuedSubmissions()); |
320 |
+ |
while (p.getActiveThreadCount() != 0 |
321 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
322 |
+ |
Thread.yield(); |
323 |
|
assertFalse(p.isShutdown()); |
324 |
|
assertFalse(p.isTerminating()); |
325 |
|
assertFalse(p.isTerminated()); |
326 |
|
assertTrue(f.isDone()); |
327 |
|
assertEquals(6765, (int) f.get()); |
328 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
329 |
|
} |
330 |
|
} |
331 |
|
|
423 |
|
done.set(true); |
424 |
|
}}); |
425 |
|
assertNull(future.get()); |
426 |
< |
assertNull(future.get(0, MILLISECONDS)); |
426 |
> |
assertNull(future.get(randomExpiredTimeout(), randomTimeUnit())); |
427 |
|
assertTrue(done.get()); |
428 |
|
assertTrue(future.isDone()); |
429 |
|
assertFalse(future.isCancelled()); |
632 |
|
public void testInvokeAny3() throws Throwable { |
633 |
|
ExecutorService e = new ForkJoinPool(1); |
634 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
635 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
635 |
> |
List<Callable<String>> l = new ArrayList<>(); |
636 |
|
l.add(null); |
637 |
|
try { |
638 |
|
e.invokeAny(l); |
648 |
|
CountDownLatch latch = new CountDownLatch(1); |
649 |
|
ExecutorService e = new ForkJoinPool(1); |
650 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
651 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
651 |
> |
List<Callable<String>> l = new ArrayList<>(); |
652 |
|
l.add(latchAwaitingStringTask(latch)); |
653 |
|
l.add(null); |
654 |
|
try { |
665 |
|
public void testInvokeAny5() throws Throwable { |
666 |
|
ExecutorService e = new ForkJoinPool(1); |
667 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
668 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
668 |
> |
List<Callable<String>> l = new ArrayList<>(); |
669 |
|
l.add(new NPETask()); |
670 |
|
try { |
671 |
|
e.invokeAny(l); |
682 |
|
public void testInvokeAny6() throws Throwable { |
683 |
|
ExecutorService e = new ForkJoinPool(1); |
684 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
685 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
685 |
> |
List<Callable<String>> l = new ArrayList<>(); |
686 |
|
l.add(new StringTask()); |
687 |
|
l.add(new StringTask()); |
688 |
|
String result = e.invokeAny(l); |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
< |
* invokeAll(empty collection) returns empty collection |
707 |
> |
* invokeAll(empty collection) returns empty list |
708 |
|
*/ |
709 |
|
public void testInvokeAll2() throws InterruptedException { |
710 |
|
ExecutorService e = new ForkJoinPool(1); |
711 |
+ |
final Collection<Callable<String>> emptyCollection |
712 |
+ |
= Collections.emptyList(); |
713 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
714 |
< |
List<Future<String>> r |
701 |
< |
= e.invokeAll(new ArrayList<Callable<String>>()); |
714 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
715 |
|
assertTrue(r.isEmpty()); |
716 |
|
} |
717 |
|
} |
722 |
|
public void testInvokeAll3() throws InterruptedException { |
723 |
|
ExecutorService e = new ForkJoinPool(1); |
724 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
725 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
725 |
> |
List<Callable<String>> l = new ArrayList<>(); |
726 |
|
l.add(new StringTask()); |
727 |
|
l.add(null); |
728 |
|
try { |
739 |
|
public void testInvokeAll4() throws Throwable { |
740 |
|
ExecutorService e = new ForkJoinPool(1); |
741 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
742 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
742 |
> |
List<Callable<String>> l = new ArrayList<>(); |
743 |
|
l.add(new NPETask()); |
744 |
|
List<Future<String>> futures = e.invokeAll(l); |
745 |
|
assertEquals(1, futures.size()); |
758 |
|
public void testInvokeAll5() throws Throwable { |
759 |
|
ExecutorService e = new ForkJoinPool(1); |
760 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
761 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
761 |
> |
List<Callable<String>> l = new ArrayList<>(); |
762 |
|
l.add(new StringTask()); |
763 |
|
l.add(new StringTask()); |
764 |
|
List<Future<String>> futures = e.invokeAll(l); |
775 |
|
ExecutorService e = new ForkJoinPool(1); |
776 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
777 |
|
try { |
778 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
778 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
779 |
|
shouldThrow(); |
780 |
|
} catch (NullPointerException success) {} |
781 |
|
} |
787 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
788 |
|
ExecutorService e = new ForkJoinPool(1); |
789 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
790 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
790 |
> |
List<Callable<String>> l = new ArrayList<>(); |
791 |
|
l.add(new StringTask()); |
792 |
|
try { |
793 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
793 |
> |
e.invokeAny(l, randomTimeout(), null); |
794 |
|
shouldThrow(); |
795 |
|
} catch (NullPointerException success) {} |
796 |
|
} |
804 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
805 |
|
try { |
806 |
|
e.invokeAny(new ArrayList<Callable<String>>(), |
807 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
807 |
> |
randomTimeout(), randomTimeUnit()); |
808 |
|
shouldThrow(); |
809 |
|
} catch (IllegalArgumentException success) {} |
810 |
|
} |
817 |
|
CountDownLatch latch = new CountDownLatch(1); |
818 |
|
ExecutorService e = new ForkJoinPool(1); |
819 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
820 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
820 |
> |
List<Callable<String>> l = new ArrayList<>(); |
821 |
|
l.add(latchAwaitingStringTask(latch)); |
822 |
|
l.add(null); |
823 |
|
try { |
824 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
824 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
825 |
|
shouldThrow(); |
826 |
|
} catch (NullPointerException success) {} |
827 |
|
latch.countDown(); |
835 |
|
ExecutorService e = new ForkJoinPool(1); |
836 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
837 |
|
long startTime = System.nanoTime(); |
838 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
838 |
> |
List<Callable<String>> l = new ArrayList<>(); |
839 |
|
l.add(new NPETask()); |
840 |
|
try { |
841 |
|
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
853 |
|
public void testTimedInvokeAny5() throws Throwable { |
854 |
|
ExecutorService e = new ForkJoinPool(1); |
855 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
856 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
856 |
> |
long startTime = System.nanoTime(); |
857 |
> |
List<Callable<String>> l = new ArrayList<>(); |
858 |
|
l.add(new StringTask()); |
859 |
|
l.add(new StringTask()); |
860 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
860 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
861 |
|
assertSame(TEST_STRING, result); |
862 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
863 |
|
} |
864 |
|
} |
865 |
|
|
870 |
|
ExecutorService e = new ForkJoinPool(1); |
871 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
872 |
|
try { |
873 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
873 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
874 |
|
shouldThrow(); |
875 |
|
} catch (NullPointerException success) {} |
876 |
|
} |
882 |
|
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
883 |
|
ExecutorService e = new ForkJoinPool(1); |
884 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
885 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
885 |
> |
List<Callable<String>> l = new ArrayList<>(); |
886 |
|
l.add(new StringTask()); |
887 |
|
try { |
888 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
888 |
> |
e.invokeAll(l, randomTimeout(), null); |
889 |
|
shouldThrow(); |
890 |
|
} catch (NullPointerException success) {} |
891 |
|
} |
892 |
|
} |
893 |
|
|
894 |
|
/** |
895 |
< |
* timed invokeAll(empty collection) returns empty collection |
895 |
> |
* timed invokeAll(empty collection) returns empty list |
896 |
|
*/ |
897 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
898 |
|
ExecutorService e = new ForkJoinPool(1); |
899 |
+ |
final Collection<Callable<String>> emptyCollection |
900 |
+ |
= Collections.emptyList(); |
901 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
902 |
|
List<Future<String>> r |
903 |
< |
= e.invokeAll(new ArrayList<Callable<String>>(), |
904 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
903 |
> |
= e.invokeAll(emptyCollection, |
904 |
> |
randomTimeout(), randomTimeUnit()); |
905 |
|
assertTrue(r.isEmpty()); |
906 |
|
} |
907 |
|
} |
912 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
913 |
|
ExecutorService e = new ForkJoinPool(1); |
914 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
915 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
915 |
> |
List<Callable<String>> l = new ArrayList<>(); |
916 |
|
l.add(new StringTask()); |
917 |
|
l.add(null); |
918 |
|
try { |
919 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
919 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
920 |
|
shouldThrow(); |
921 |
|
} catch (NullPointerException success) {} |
922 |
|
} |
928 |
|
public void testTimedInvokeAll4() throws Throwable { |
929 |
|
ExecutorService e = new ForkJoinPool(1); |
930 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
931 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
931 |
> |
List<Callable<String>> l = new ArrayList<>(); |
932 |
|
l.add(new NPETask()); |
933 |
|
List<Future<String>> futures |
934 |
|
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
948 |
|
public void testTimedInvokeAll5() throws Throwable { |
949 |
|
ForkJoinPool e = new ForkJoinPool(1); |
950 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
951 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
951 |
> |
List<Callable<String>> l = new ArrayList<>(); |
952 |
|
l.add(new StringTask()); |
953 |
|
l.add(new StringTask()); |
954 |
|
List<Future<String>> futures |