28 |
|
import java.util.concurrent.RejectedExecutionHandler; |
29 |
|
import java.util.concurrent.SynchronousQueue; |
30 |
|
import java.util.concurrent.ThreadFactory; |
31 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
32 |
|
import java.util.concurrent.ThreadPoolExecutor; |
33 |
+ |
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; |
34 |
+ |
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; |
35 |
+ |
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; |
36 |
+ |
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy; |
37 |
|
import java.util.concurrent.atomic.AtomicInteger; |
38 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
39 |
|
|
40 |
|
import junit.framework.Test; |
41 |
|
import junit.framework.TestSuite; |
290 |
|
LONG_DELAY_MS, MILLISECONDS, |
291 |
|
new ArrayBlockingQueue<Runnable>(10)); |
292 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
293 |
< |
assertTrue(p.getRejectedExecutionHandler() |
288 |
< |
instanceof ThreadPoolExecutor.AbortPolicy); |
293 |
> |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
294 |
|
} |
295 |
|
} |
296 |
|
|
1066 |
|
} |
1067 |
|
|
1068 |
|
/** |
1069 |
< |
* execute throws RejectedExecutionException if saturated. |
1069 |
> |
* Submitted tasks are rejected when saturated. |
1070 |
|
*/ |
1071 |
< |
public void testSaturatedExecute() { |
1071 |
> |
@SuppressWarnings("FutureReturnValueIgnored") |
1072 |
> |
public void testSubmittedTasksRejectedWhenSaturated() { |
1073 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1074 |
|
final CountDownLatch done = new CountDownLatch(1); |
1075 |
< |
final ThreadPoolExecutor p = |
1076 |
< |
new ThreadPoolExecutor(1, 1, |
1077 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1078 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
1075 |
> |
final Runnable r = awaiter(done); |
1076 |
> |
final Callable<Boolean> c = new CheckedCallable() { |
1077 |
> |
public Boolean realCall() throws InterruptedException { |
1078 |
> |
await(done); |
1079 |
> |
return Boolean.TRUE; |
1080 |
> |
}}; |
1081 |
> |
final ThreadPoolExecutor p = new ThreadPoolExecutor( |
1082 |
> |
1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1083 |
> |
|
1084 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1085 |
< |
Runnable task = new CheckedRunnable() { |
1086 |
< |
public void realRun() throws InterruptedException { |
1087 |
< |
await(done); |
1088 |
< |
}}; |
1089 |
< |
for (int i = 0; i < 2; ++i) |
1090 |
< |
p.execute(task); |
1091 |
< |
for (int i = 0; i < 2; ++i) { |
1085 |
> |
// saturate |
1086 |
> |
for (int i = saturatedSize(p); i--> 0; ) { |
1087 |
> |
switch (rnd.nextInt(3)) { |
1088 |
> |
case 0: p.execute(r); break; |
1089 |
> |
case 1: assertFalse(p.submit(r).isDone()); break; |
1090 |
> |
case 2: assertFalse(p.submit(c).isDone()); break; |
1091 |
> |
} |
1092 |
> |
} |
1093 |
> |
|
1094 |
> |
// check default handler |
1095 |
> |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
1096 |
> |
for (int i = 2; i--> 0; ) { |
1097 |
|
try { |
1098 |
< |
p.execute(task); |
1098 |
> |
p.execute(r); |
1099 |
|
shouldThrow(); |
1100 |
|
} catch (RejectedExecutionException success) {} |
1084 |
– |
assertTrue(p.getTaskCount() <= 2); |
1085 |
– |
} |
1086 |
– |
} |
1087 |
– |
} |
1088 |
– |
|
1089 |
– |
/** |
1090 |
– |
* submit(runnable) throws RejectedExecutionException if saturated. |
1091 |
– |
*/ |
1092 |
– |
public void testSaturatedSubmitRunnable() { |
1093 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1094 |
– |
final ThreadPoolExecutor p = |
1095 |
– |
new ThreadPoolExecutor(1, 1, |
1096 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1097 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1098 |
– |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1099 |
– |
Runnable task = new CheckedRunnable() { |
1100 |
– |
public void realRun() throws InterruptedException { |
1101 |
– |
await(done); |
1102 |
– |
}}; |
1103 |
– |
for (int i = 0; i < 2; ++i) |
1104 |
– |
p.submit(task); |
1105 |
– |
for (int i = 0; i < 2; ++i) { |
1101 |
|
try { |
1102 |
< |
p.execute(task); |
1102 |
> |
p.submit(r); |
1103 |
|
shouldThrow(); |
1104 |
|
} catch (RejectedExecutionException success) {} |
1110 |
– |
assertTrue(p.getTaskCount() <= 2); |
1111 |
– |
} |
1112 |
– |
} |
1113 |
– |
} |
1114 |
– |
|
1115 |
– |
/** |
1116 |
– |
* submit(callable) throws RejectedExecutionException if saturated. |
1117 |
– |
*/ |
1118 |
– |
public void testSaturatedSubmitCallable() { |
1119 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1120 |
– |
final ThreadPoolExecutor p = |
1121 |
– |
new ThreadPoolExecutor(1, 1, |
1122 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1123 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1124 |
– |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1125 |
– |
Runnable task = new CheckedRunnable() { |
1126 |
– |
public void realRun() throws InterruptedException { |
1127 |
– |
await(done); |
1128 |
– |
}}; |
1129 |
– |
for (int i = 0; i < 2; ++i) |
1130 |
– |
p.execute(task); |
1131 |
– |
for (int i = 0; i < 2; ++i) { |
1105 |
|
try { |
1106 |
< |
p.execute(task); |
1106 |
> |
p.submit(c); |
1107 |
|
shouldThrow(); |
1108 |
|
} catch (RejectedExecutionException success) {} |
1136 |
– |
assertTrue(p.getTaskCount() <= 2); |
1109 |
|
} |
1110 |
+ |
|
1111 |
+ |
// check CallerRunsPolicy runs task in caller thread |
1112 |
+ |
{ |
1113 |
+ |
RejectedExecutionHandler handler = new CallerRunsPolicy(); |
1114 |
+ |
p.setRejectedExecutionHandler(handler); |
1115 |
+ |
assertSame(handler, p.getRejectedExecutionHandler()); |
1116 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
1117 |
+ |
p.execute(new Runnable() { public void run() { |
1118 |
+ |
thread.set(Thread.currentThread()); }}); |
1119 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
1120 |
+ |
} |
1121 |
+ |
|
1122 |
+ |
// check DiscardPolicy does nothing |
1123 |
+ |
{ |
1124 |
+ |
RejectedExecutionHandler handler = new DiscardPolicy(); |
1125 |
+ |
p.setRejectedExecutionHandler(handler); |
1126 |
+ |
assertSame(handler, p.getRejectedExecutionHandler()); |
1127 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
1128 |
+ |
p.execute(new Runnable() { public void run() { |
1129 |
+ |
thread.set(Thread.currentThread()); }}); |
1130 |
+ |
assertNull(thread.get()); |
1131 |
+ |
} |
1132 |
+ |
|
1133 |
+ |
class Recorder implements RejectedExecutionHandler { |
1134 |
+ |
public volatile Runnable r = null; |
1135 |
+ |
public volatile ThreadPoolExecutor p = null; |
1136 |
+ |
public void reset() { r = null; p = null; } |
1137 |
+ |
public void rejectedExecution(Runnable r, ThreadPoolExecutor p) { |
1138 |
+ |
assertNull(this.r); |
1139 |
+ |
assertNull(this.p); |
1140 |
+ |
this.r = r; |
1141 |
+ |
this.p = p; |
1142 |
+ |
} |
1143 |
+ |
} |
1144 |
+ |
|
1145 |
+ |
// check custom handler is invoked exactly once per task |
1146 |
+ |
Recorder recorder = new Recorder(); |
1147 |
+ |
p.setRejectedExecutionHandler(recorder); |
1148 |
+ |
assertSame(recorder, p.getRejectedExecutionHandler()); |
1149 |
+ |
for (int i = 2; i--> 0; ) { |
1150 |
+ |
recorder.reset(); |
1151 |
+ |
p.execute(r); |
1152 |
+ |
assertSame(r, recorder.r); |
1153 |
+ |
assertSame(p, recorder.p); |
1154 |
+ |
|
1155 |
+ |
recorder.reset(); |
1156 |
+ |
assertFalse(p.submit(r).isDone()); |
1157 |
+ |
assertTrue(recorder.r instanceof FutureTask); |
1158 |
+ |
assertSame(p, recorder.p); |
1159 |
+ |
|
1160 |
+ |
recorder.reset(); |
1161 |
+ |
assertFalse(p.submit(c).isDone()); |
1162 |
+ |
assertTrue(recorder.r instanceof FutureTask); |
1163 |
+ |
assertSame(p, recorder.p); |
1164 |
+ |
} |
1165 |
+ |
|
1166 |
+ |
// check that pool was not perturbed by handlers |
1167 |
+ |
assertEquals(2, p.getTaskCount()); |
1168 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
1169 |
+ |
assertEquals(0, p.getQueue().remainingCapacity()); |
1170 |
|
} |
1171 |
+ |
assertEquals(saturatedSize(p), p.getCompletedTaskCount()); |
1172 |
|
} |
1173 |
|
|
1174 |
|
/** |
1175 |
|
* executor using CallerRunsPolicy runs task if saturated. |
1176 |
|
*/ |
1177 |
|
public void testSaturatedExecute2() { |
1178 |
< |
final ThreadPoolExecutor p = |
1179 |
< |
new ThreadPoolExecutor(1, 1, |
1180 |
< |
LONG_DELAY_MS, |
1181 |
< |
MILLISECONDS, |
1182 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1183 |
< |
new ThreadPoolExecutor.CallerRunsPolicy()); |
1184 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1185 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1186 |
< |
Runnable blocker = new CheckedRunnable() { |
1187 |
< |
public void realRun() throws InterruptedException { |
1155 |
< |
await(done); |
1156 |
< |
}}; |
1157 |
< |
p.execute(blocker); |
1158 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1159 |
< |
for (int i = 0; i < tasks.length; i++) |
1160 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1178 |
> |
final RejectedExecutionHandler handler = new CallerRunsPolicy(); |
1179 |
> |
final ThreadPoolExecutor p = new ThreadPoolExecutor( |
1180 |
> |
1, 1, LONG_DELAY_MS, SECONDS, new ArrayBlockingQueue<Runnable>(1), |
1181 |
> |
handler); |
1182 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
1183 |
> |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1184 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1185 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1186 |
> |
p.execute(awaiter(done)); |
1187 |
> |
|
1188 |
|
for (int i = 0; i < tasks.length; i++) |
1189 |
< |
p.execute(tasks[i]); |
1189 |
> |
p.execute(tasks[i] = new TrackedNoOpRunnable()); |
1190 |
> |
|
1191 |
|
for (int i = 1; i < tasks.length; i++) |
1192 |
|
assertTrue(tasks[i].done); |
1193 |
|
assertFalse(tasks[0].done); // waiting in queue |
1166 |
– |
done.countDown(); |
1194 |
|
} |
1195 |
+ |
for (TrackedNoOpRunnable task : tasks) |
1196 |
+ |
assertTrue(task.done); |
1197 |
|
} |
1198 |
|
|
1199 |
|
/** |
1200 |
|
* executor using DiscardPolicy drops task if saturated. |
1201 |
|
*/ |
1202 |
|
public void testSaturatedExecute3() { |
1203 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1203 |
> |
final RejectedExecutionHandler handler = new DiscardPolicy(); |
1204 |
> |
final ThreadPoolExecutor p = new ThreadPoolExecutor( |
1205 |
> |
1, 1, LONG_DELAY_MS, SECONDS, new ArrayBlockingQueue<Runnable>(1), |
1206 |
> |
handler); |
1207 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
1208 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1209 |
< |
for (int i = 0; i < tasks.length; ++i) |
1177 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1178 |
< |
final ThreadPoolExecutor p = |
1179 |
< |
new ThreadPoolExecutor(1, 1, |
1180 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1181 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1182 |
< |
new ThreadPoolExecutor.DiscardPolicy()); |
1209 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1210 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1211 |
|
p.execute(awaiter(done)); |
1212 |
|
|
1213 |
< |
for (TrackedNoOpRunnable task : tasks) |
1214 |
< |
p.execute(task); |
1213 |
> |
for (int i = 0; i < tasks.length; i++) |
1214 |
> |
p.execute(tasks[i] = new TrackedNoOpRunnable()); |
1215 |
> |
|
1216 |
|
for (int i = 1; i < tasks.length; i++) |
1217 |
|
assertFalse(tasks[i].done); |
1218 |
|
} |
1233 |
|
new ThreadPoolExecutor(1, 1, |
1234 |
|
LONG_DELAY_MS, MILLISECONDS, |
1235 |
|
new ArrayBlockingQueue<Runnable>(1), |
1236 |
< |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1236 |
> |
new DiscardOldestPolicy()); |
1237 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1238 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1239 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1271 |
|
* execute using CallerRunsPolicy drops task on shutdown |
1272 |
|
*/ |
1273 |
|
public void testCallerRunsOnShutdown() { |
1274 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1274 |
> |
RejectedExecutionHandler h = new CallerRunsPolicy(); |
1275 |
|
final ThreadPoolExecutor p = |
1276 |
|
new ThreadPoolExecutor(1, 1, |
1277 |
|
LONG_DELAY_MS, MILLISECONDS, |
1293 |
|
new ThreadPoolExecutor(1, 1, |
1294 |
|
LONG_DELAY_MS, MILLISECONDS, |
1295 |
|
new ArrayBlockingQueue<Runnable>(1), |
1296 |
< |
new ThreadPoolExecutor.DiscardPolicy()); |
1296 |
> |
new DiscardPolicy()); |
1297 |
|
|
1298 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1299 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1311 |
|
new ThreadPoolExecutor(1, 1, |
1312 |
|
LONG_DELAY_MS, MILLISECONDS, |
1313 |
|
new ArrayBlockingQueue<Runnable>(1), |
1314 |
< |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1314 |
> |
new DiscardOldestPolicy()); |
1315 |
|
|
1316 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1317 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
2109 |
|
} |
2110 |
|
} |
2111 |
|
|
2112 |
+ |
/** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */ |
2113 |
+ |
public void testStandardRejectedExecutionHandlers() { |
2114 |
+ |
final ThreadPoolExecutor p = |
2115 |
+ |
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
2116 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
2117 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2118 |
+ |
final Runnable r = new Runnable() { public void run() { |
2119 |
+ |
thread.set(Thread.currentThread()); }}; |
2120 |
+ |
|
2121 |
+ |
try { |
2122 |
+ |
new AbortPolicy().rejectedExecution(r, p); |
2123 |
+ |
shouldThrow(); |
2124 |
+ |
} catch (RejectedExecutionException success) {} |
2125 |
+ |
assertNull(thread.get()); |
2126 |
+ |
|
2127 |
+ |
new DiscardPolicy().rejectedExecution(r, p); |
2128 |
+ |
assertNull(thread.get()); |
2129 |
+ |
|
2130 |
+ |
new CallerRunsPolicy().rejectedExecution(r, p); |
2131 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
2132 |
+ |
|
2133 |
+ |
// check that pool was not perturbed by handlers |
2134 |
+ |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
2135 |
+ |
assertEquals(0, p.getTaskCount()); |
2136 |
+ |
assertTrue(p.getQueue().isEmpty()); |
2137 |
+ |
} |
2138 |
+ |
|
2139 |
|
} |