76 |
|
import java.util.concurrent.CountDownLatch; |
77 |
|
import java.util.concurrent.CyclicBarrier; |
78 |
|
import java.util.concurrent.ExecutionException; |
79 |
+ |
import java.util.concurrent.Executor; |
80 |
|
import java.util.concurrent.Executors; |
81 |
|
import java.util.concurrent.ExecutorService; |
82 |
|
import java.util.concurrent.ForkJoinPool; |
83 |
|
import java.util.concurrent.Future; |
84 |
+ |
import java.util.concurrent.FutureTask; |
85 |
|
import java.util.concurrent.RecursiveAction; |
86 |
|
import java.util.concurrent.RecursiveTask; |
87 |
+ |
import java.util.concurrent.RejectedExecutionException; |
88 |
|
import java.util.concurrent.RejectedExecutionHandler; |
89 |
|
import java.util.concurrent.Semaphore; |
90 |
+ |
import java.util.concurrent.ScheduledExecutorService; |
91 |
+ |
import java.util.concurrent.ScheduledFuture; |
92 |
|
import java.util.concurrent.SynchronousQueue; |
93 |
|
import java.util.concurrent.ThreadFactory; |
94 |
|
import java.util.concurrent.ThreadLocalRandom; |
1959 |
|
static <T> void shuffle(T[] array) { |
1960 |
|
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1961 |
|
} |
1962 |
+ |
|
1963 |
+ |
// --- Shared assertions for Executor tests --- |
1964 |
+ |
|
1965 |
+ |
/** |
1966 |
+ |
* Returns maximum number of tasks that can be submitted to given |
1967 |
+ |
* pool (with bounded queue) before saturation (when submission |
1968 |
+ |
* throws RejectedExecutionException). |
1969 |
+ |
*/ |
1970 |
+ |
static final int saturatedSize(ThreadPoolExecutor pool) { |
1971 |
+ |
BlockingQueue<Runnable> q = pool.getQueue(); |
1972 |
+ |
return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity(); |
1973 |
+ |
} |
1974 |
+ |
|
1975 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1976 |
+ |
void assertNullTaskSubmissionThrowsNullPointerException(Executor e) { |
1977 |
+ |
try { |
1978 |
+ |
e.execute((Runnable) null); |
1979 |
+ |
shouldThrow(); |
1980 |
+ |
} catch (NullPointerException success) {} |
1981 |
+ |
|
1982 |
+ |
if (! (e instanceof ExecutorService)) return; |
1983 |
+ |
ExecutorService es = (ExecutorService) e; |
1984 |
+ |
try { |
1985 |
+ |
es.submit((Runnable) null); |
1986 |
+ |
shouldThrow(); |
1987 |
+ |
} catch (NullPointerException success) {} |
1988 |
+ |
try { |
1989 |
+ |
es.submit((Runnable) null, Boolean.TRUE); |
1990 |
+ |
shouldThrow(); |
1991 |
+ |
} catch (NullPointerException success) {} |
1992 |
+ |
try { |
1993 |
+ |
es.submit((Callable) null); |
1994 |
+ |
shouldThrow(); |
1995 |
+ |
} catch (NullPointerException success) {} |
1996 |
+ |
|
1997 |
+ |
if (! (e instanceof ScheduledExecutorService)) return; |
1998 |
+ |
ScheduledExecutorService ses = (ScheduledExecutorService) e; |
1999 |
+ |
try { |
2000 |
+ |
ses.schedule((Runnable) null, |
2001 |
+ |
randomTimeout(), randomTimeUnit()); |
2002 |
+ |
shouldThrow(); |
2003 |
+ |
} catch (NullPointerException success) {} |
2004 |
+ |
try { |
2005 |
+ |
ses.schedule((Callable) null, |
2006 |
+ |
randomTimeout(), randomTimeUnit()); |
2007 |
+ |
shouldThrow(); |
2008 |
+ |
} catch (NullPointerException success) {} |
2009 |
+ |
try { |
2010 |
+ |
ses.scheduleAtFixedRate((Runnable) null, |
2011 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2012 |
+ |
shouldThrow(); |
2013 |
+ |
} catch (NullPointerException success) {} |
2014 |
+ |
try { |
2015 |
+ |
ses.scheduleWithFixedDelay((Runnable) null, |
2016 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2017 |
+ |
shouldThrow(); |
2018 |
+ |
} catch (NullPointerException success) {} |
2019 |
+ |
} |
2020 |
+ |
|
2021 |
+ |
void setRejectedExecutionHandler( |
2022 |
+ |
ThreadPoolExecutor p, RejectedExecutionHandler handler) { |
2023 |
+ |
p.setRejectedExecutionHandler(handler); |
2024 |
+ |
assertSame(handler, p.getRejectedExecutionHandler()); |
2025 |
+ |
} |
2026 |
+ |
|
2027 |
+ |
void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) { |
2028 |
+ |
final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler(); |
2029 |
+ |
final long savedTaskCount = p.getTaskCount(); |
2030 |
+ |
final long savedCompletedTaskCount = p.getCompletedTaskCount(); |
2031 |
+ |
final int savedQueueSize = p.getQueue().size(); |
2032 |
+ |
final boolean stock = (p.getClass().getClassLoader() == null); |
2033 |
+ |
|
2034 |
+ |
Runnable r = () -> {}; |
2035 |
+ |
Callable<Boolean> c = () -> Boolean.TRUE; |
2036 |
+ |
|
2037 |
+ |
class Recorder implements RejectedExecutionHandler { |
2038 |
+ |
public volatile Runnable r = null; |
2039 |
+ |
public volatile ThreadPoolExecutor p = null; |
2040 |
+ |
public void reset() { r = null; p = null; } |
2041 |
+ |
public void rejectedExecution(Runnable r, ThreadPoolExecutor p) { |
2042 |
+ |
assertNull(this.r); |
2043 |
+ |
assertNull(this.p); |
2044 |
+ |
this.r = r; |
2045 |
+ |
this.p = p; |
2046 |
+ |
} |
2047 |
+ |
} |
2048 |
+ |
|
2049 |
+ |
// check custom handler is invoked exactly once per task |
2050 |
+ |
Recorder recorder = new Recorder(); |
2051 |
+ |
setRejectedExecutionHandler(p, recorder); |
2052 |
+ |
for (int i = 2; i--> 0; ) { |
2053 |
+ |
recorder.reset(); |
2054 |
+ |
p.execute(r); |
2055 |
+ |
if (stock && p.getClass() == ThreadPoolExecutor.class) |
2056 |
+ |
assertSame(r, recorder.r); |
2057 |
+ |
assertSame(p, recorder.p); |
2058 |
+ |
|
2059 |
+ |
recorder.reset(); |
2060 |
+ |
assertFalse(p.submit(r).isDone()); |
2061 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2062 |
+ |
assertSame(p, recorder.p); |
2063 |
+ |
|
2064 |
+ |
recorder.reset(); |
2065 |
+ |
assertFalse(p.submit(r, Boolean.TRUE).isDone()); |
2066 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2067 |
+ |
assertSame(p, recorder.p); |
2068 |
+ |
|
2069 |
+ |
recorder.reset(); |
2070 |
+ |
assertFalse(p.submit(c).isDone()); |
2071 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2072 |
+ |
assertSame(p, recorder.p); |
2073 |
+ |
|
2074 |
+ |
if (p instanceof ScheduledExecutorService) { |
2075 |
+ |
ScheduledExecutorService s = (ScheduledExecutorService) p; |
2076 |
+ |
ScheduledFuture<?> future; |
2077 |
+ |
|
2078 |
+ |
recorder.reset(); |
2079 |
+ |
future = s.schedule(r, randomTimeout(), randomTimeUnit()); |
2080 |
+ |
assertFalse(future.isDone()); |
2081 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2082 |
+ |
assertSame(p, recorder.p); |
2083 |
+ |
|
2084 |
+ |
recorder.reset(); |
2085 |
+ |
future = s.schedule(c, randomTimeout(), randomTimeUnit()); |
2086 |
+ |
assertFalse(future.isDone()); |
2087 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2088 |
+ |
assertSame(p, recorder.p); |
2089 |
+ |
|
2090 |
+ |
recorder.reset(); |
2091 |
+ |
future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2092 |
+ |
assertFalse(future.isDone()); |
2093 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2094 |
+ |
assertSame(p, recorder.p); |
2095 |
+ |
|
2096 |
+ |
recorder.reset(); |
2097 |
+ |
future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2098 |
+ |
assertFalse(future.isDone()); |
2099 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2100 |
+ |
assertSame(p, recorder.p); |
2101 |
+ |
} |
2102 |
+ |
} |
2103 |
+ |
|
2104 |
+ |
// Checking our custom handler above should be sufficient, but |
2105 |
+ |
// we add some integration tests of standard handlers. |
2106 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2107 |
+ |
final Runnable setThread = () -> thread.set(Thread.currentThread()); |
2108 |
+ |
|
2109 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy()); |
2110 |
+ |
try { |
2111 |
+ |
p.execute(setThread); |
2112 |
+ |
shouldThrow(); |
2113 |
+ |
} catch (RejectedExecutionException success) {} |
2114 |
+ |
assertNull(thread.get()); |
2115 |
+ |
|
2116 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy()); |
2117 |
+ |
p.execute(setThread); |
2118 |
+ |
assertNull(thread.get()); |
2119 |
+ |
|
2120 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy()); |
2121 |
+ |
p.execute(setThread); |
2122 |
+ |
if (p.isShutdown()) |
2123 |
+ |
assertNull(thread.get()); |
2124 |
+ |
else |
2125 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
2126 |
+ |
|
2127 |
+ |
setRejectedExecutionHandler(p, savedHandler); |
2128 |
+ |
|
2129 |
+ |
// check that pool was not perturbed by handlers |
2130 |
+ |
assertEquals(savedTaskCount, p.getTaskCount()); |
2131 |
+ |
assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount()); |
2132 |
+ |
assertEquals(savedQueueSize, p.getQueue().size()); |
2133 |
+ |
} |
2134 |
|
} |