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 |
|
|
563 |
|
q); |
564 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
565 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
566 |
< |
FutureTask[] tasks = new FutureTask[5]; |
566 |
> |
FutureTask[] rtasks = new FutureTask[5]; |
567 |
> |
@SuppressWarnings("unchecked") |
568 |
> |
FutureTask<Boolean>[] tasks = (FutureTask<Boolean>[])rtasks; |
569 |
|
for (int i = 0; i < tasks.length; i++) { |
570 |
< |
Callable task = new CheckedCallable<Boolean>() { |
570 |
> |
Callable<Boolean> task = new CheckedCallable<>() { |
571 |
|
public Boolean realCall() throws InterruptedException { |
572 |
|
threadStarted.countDown(); |
573 |
|
assertSame(q, p.getQueue()); |
574 |
|
await(done); |
575 |
|
return Boolean.TRUE; |
576 |
|
}}; |
577 |
< |
tasks[i] = new FutureTask(task); |
577 |
> |
tasks[i] = new FutureTask<>(task); |
578 |
|
p.execute(tasks[i]); |
579 |
|
} |
580 |
|
await(threadStarted); |
631 |
|
LONG_DELAY_MS, MILLISECONDS, |
632 |
|
q); |
633 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
634 |
< |
FutureTask[] tasks = new FutureTask[5]; |
634 |
> |
FutureTask[] rtasks = new FutureTask[5]; |
635 |
> |
@SuppressWarnings("unchecked") |
636 |
> |
FutureTask<Boolean>[] tasks = (FutureTask<Boolean>[])rtasks; |
637 |
|
for (int i = 0; i < tasks.length; i++) { |
638 |
< |
Callable task = new CheckedCallable<Boolean>() { |
638 |
> |
Callable<Boolean> task = new CheckedCallable<>() { |
639 |
|
public Boolean realCall() throws InterruptedException { |
640 |
|
threadStarted.countDown(); |
641 |
|
await(done); |
642 |
|
return Boolean.TRUE; |
643 |
|
}}; |
644 |
< |
tasks[i] = new FutureTask(task); |
644 |
> |
tasks[i] = new FutureTask<>(task); |
645 |
|
p.execute(tasks[i]); |
646 |
|
} |
647 |
|
await(threadStarted); |
676 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
677 |
|
threadsStarted.countDown(); |
678 |
|
try { |
679 |
< |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
679 |
> |
MILLISECONDS.sleep(LONGER_DELAY_MS); |
680 |
|
} catch (InterruptedException success) {} |
681 |
|
ran.getAndIncrement(); |
682 |
|
}}; |
763 |
|
public void testConstructorNullPointerException() { |
764 |
|
try { |
765 |
|
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
766 |
< |
(BlockingQueue) null); |
766 |
> |
(BlockingQueue<Runnable>) null); |
767 |
|
shouldThrow(); |
768 |
|
} catch (NullPointerException success) {} |
769 |
|
} |
834 |
|
public void testConstructorNullPointerException2() { |
835 |
|
try { |
836 |
|
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
837 |
< |
(BlockingQueue) null, |
837 |
> |
(BlockingQueue<Runnable>) null, |
838 |
|
new SimpleThreadFactory()); |
839 |
|
shouldThrow(); |
840 |
|
} catch (NullPointerException success) {} |
918 |
|
public void testConstructorNullPointerException4() { |
919 |
|
try { |
920 |
|
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
921 |
< |
(BlockingQueue) null, |
921 |
> |
(BlockingQueue<Runnable>) null, |
922 |
|
new NoOpREHandler()); |
923 |
|
shouldThrow(); |
924 |
|
} catch (NullPointerException success) {} |
1007 |
|
public void testConstructorNullPointerException6() { |
1008 |
|
try { |
1009 |
|
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1010 |
< |
(BlockingQueue) null, |
1010 |
> |
(BlockingQueue<Runnable>) null, |
1011 |
|
new SimpleThreadFactory(), |
1012 |
|
new NoOpREHandler()); |
1013 |
|
shouldThrow(); |
1054 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1055 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1056 |
|
public void realRun() throws Exception { |
1057 |
< |
Callable task = new CheckedCallable<Boolean>() { |
1057 |
> |
Callable<Boolean> task = new CheckedCallable<>() { |
1058 |
|
public Boolean realCall() throws InterruptedException { |
1059 |
|
threadStarted.countDown(); |
1060 |
|
await(done); |
1063 |
|
p.submit(task).get(); |
1064 |
|
}}); |
1065 |
|
|
1066 |
< |
await(threadStarted); |
1066 |
> |
await(threadStarted); // ensure quiescence |
1067 |
|
t.interrupt(); |
1068 |
|
awaitTermination(t); |
1069 |
|
} |
1070 |
|
} |
1071 |
|
|
1072 |
|
/** |
1073 |
< |
* execute throws RejectedExecutionException if saturated. |
1073 |
> |
* Submitted tasks are rejected when saturated or shutdown |
1074 |
|
*/ |
1075 |
< |
public void testSaturatedExecute() { |
1075 |
> |
public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException { |
1076 |
> |
final ThreadPoolExecutor p = new ThreadPoolExecutor( |
1077 |
> |
1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1078 |
> |
final int saturatedSize = saturatedSize(p); |
1079 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1080 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize()); |
1081 |
|
final CountDownLatch done = new CountDownLatch(1); |
1082 |
< |
final ThreadPoolExecutor p = |
1083 |
< |
new ThreadPoolExecutor(1, 1, |
1084 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1071 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
1072 |
< |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1073 |
< |
Runnable task = new CheckedRunnable() { |
1074 |
< |
public void realRun() throws InterruptedException { |
1075 |
< |
await(done); |
1076 |
< |
}}; |
1077 |
< |
for (int i = 0; i < 2; ++i) |
1078 |
< |
p.execute(task); |
1079 |
< |
for (int i = 0; i < 2; ++i) { |
1082 |
> |
final Runnable r = () -> { |
1083 |
> |
threadsStarted.countDown(); |
1084 |
> |
for (;;) { |
1085 |
|
try { |
1086 |
< |
p.execute(task); |
1087 |
< |
shouldThrow(); |
1088 |
< |
} catch (RejectedExecutionException success) {} |
1089 |
< |
assertTrue(p.getTaskCount() <= 2); |
1090 |
< |
} |
1091 |
< |
} |
1092 |
< |
} |
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) { |
1086 |
> |
done.await(); |
1087 |
> |
return; |
1088 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1089 |
> |
}}; |
1090 |
> |
final Callable<Boolean> c = () -> { |
1091 |
> |
threadsStarted.countDown(); |
1092 |
> |
for (;;) { |
1093 |
|
try { |
1094 |
< |
p.execute(task); |
1095 |
< |
shouldThrow(); |
1096 |
< |
} catch (RejectedExecutionException success) {} |
1097 |
< |
assertTrue(p.getTaskCount() <= 2); |
1098 |
< |
} |
1112 |
< |
} |
1113 |
< |
} |
1094 |
> |
done.await(); |
1095 |
> |
return Boolean.TRUE; |
1096 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1097 |
> |
}}; |
1098 |
> |
final boolean shutdownNow = rnd.nextBoolean(); |
1099 |
|
|
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)); |
1100 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1101 |
< |
Runnable task = new CheckedRunnable() { |
1102 |
< |
public void realRun() throws InterruptedException { |
1103 |
< |
await(done); |
1104 |
< |
}}; |
1105 |
< |
for (int i = 0; i < 2; ++i) |
1106 |
< |
p.execute(task); |
1107 |
< |
for (int i = 0; i < 2; ++i) { |
1108 |
< |
try { |
1133 |
< |
p.execute(task); |
1134 |
< |
shouldThrow(); |
1135 |
< |
} catch (RejectedExecutionException success) {} |
1136 |
< |
assertTrue(p.getTaskCount() <= 2); |
1101 |
> |
// saturate |
1102 |
> |
for (int i = saturatedSize; i--> 0; ) { |
1103 |
> |
switch (rnd.nextInt(4)) { |
1104 |
> |
case 0: p.execute(r); break; |
1105 |
> |
case 1: assertFalse(p.submit(r).isDone()); break; |
1106 |
> |
case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; |
1107 |
> |
case 3: assertFalse(p.submit(c).isDone()); break; |
1108 |
> |
} |
1109 |
|
} |
1138 |
– |
} |
1139 |
– |
} |
1110 |
|
|
1111 |
< |
/** |
1112 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1143 |
< |
*/ |
1144 |
< |
public void testSaturatedExecute2() { |
1145 |
< |
final ThreadPoolExecutor p = |
1146 |
< |
new ThreadPoolExecutor(1, 1, |
1147 |
< |
LONG_DELAY_MS, |
1148 |
< |
MILLISECONDS, |
1149 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1150 |
< |
new ThreadPoolExecutor.CallerRunsPolicy()); |
1151 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1152 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1153 |
< |
Runnable blocker = new CheckedRunnable() { |
1154 |
< |
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(); |
1161 |
< |
for (int i = 0; i < tasks.length; i++) |
1162 |
< |
p.execute(tasks[i]); |
1163 |
< |
for (int i = 1; i < tasks.length; i++) |
1164 |
< |
assertTrue(tasks[i].done); |
1165 |
< |
assertFalse(tasks[0].done); // waiting in queue |
1166 |
< |
done.countDown(); |
1167 |
< |
} |
1168 |
< |
} |
1111 |
> |
await(threadsStarted); |
1112 |
> |
assertTaskSubmissionsAreRejected(p); |
1113 |
|
|
1114 |
< |
/** |
1115 |
< |
* executor using DiscardPolicy drops task if saturated. |
1116 |
< |
*/ |
1117 |
< |
public void testSaturatedExecute3() { |
1118 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1119 |
< |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1120 |
< |
for (int i = 0; i < tasks.length; ++i) |
1121 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1122 |
< |
final ThreadPoolExecutor p = |
1123 |
< |
new ThreadPoolExecutor(1, 1, |
1180 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1181 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1182 |
< |
new ThreadPoolExecutor.DiscardPolicy()); |
1183 |
< |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1184 |
< |
p.execute(awaiter(done)); |
1114 |
> |
if (shutdownNow) |
1115 |
> |
p.shutdownNow(); |
1116 |
> |
else |
1117 |
> |
p.shutdown(); |
1118 |
> |
// Pool is shutdown, but not yet terminated |
1119 |
> |
assertTaskSubmissionsAreRejected(p); |
1120 |
> |
assertFalse(p.isTerminated()); |
1121 |
> |
|
1122 |
> |
done.countDown(); // release blocking tasks |
1123 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
1124 |
|
|
1125 |
< |
for (TrackedNoOpRunnable task : tasks) |
1126 |
< |
p.execute(task); |
1127 |
< |
for (int i = 1; i < tasks.length; i++) |
1128 |
< |
assertFalse(tasks[i].done); |
1129 |
< |
} |
1191 |
< |
for (int i = 1; i < tasks.length; i++) |
1192 |
< |
assertFalse(tasks[i].done); |
1193 |
< |
assertTrue(tasks[0].done); // was waiting in queue |
1125 |
> |
assertTaskSubmissionsAreRejected(p); |
1126 |
> |
} |
1127 |
> |
assertEquals(saturatedSize(p) |
1128 |
> |
- (shutdownNow ? p.getQueue().remainingCapacity() : 0), |
1129 |
> |
p.getCompletedTaskCount()); |
1130 |
|
} |
1131 |
|
|
1132 |
|
/** |
1133 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1134 |
|
*/ |
1135 |
< |
public void testSaturatedExecute4() { |
1135 |
> |
public void testSaturatedExecute_DiscardOldestPolicy() { |
1136 |
|
final CountDownLatch done = new CountDownLatch(1); |
1137 |
|
LatchAwaiter r1 = awaiter(done); |
1138 |
|
LatchAwaiter r2 = awaiter(done); |
1141 |
|
new ThreadPoolExecutor(1, 1, |
1142 |
|
LONG_DELAY_MS, MILLISECONDS, |
1143 |
|
new ArrayBlockingQueue<Runnable>(1), |
1144 |
< |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1144 |
> |
new DiscardOldestPolicy()); |
1145 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1146 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1147 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1159 |
|
} |
1160 |
|
|
1161 |
|
/** |
1226 |
– |
* execute throws RejectedExecutionException if shutdown |
1227 |
– |
*/ |
1228 |
– |
public void testRejectedExecutionExceptionOnShutdown() { |
1229 |
– |
final ThreadPoolExecutor p = |
1230 |
– |
new ThreadPoolExecutor(1, 1, |
1231 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1232 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1233 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1234 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1235 |
– |
try { |
1236 |
– |
p.execute(new NoOpRunnable()); |
1237 |
– |
shouldThrow(); |
1238 |
– |
} catch (RejectedExecutionException success) {} |
1239 |
– |
} |
1240 |
– |
} |
1241 |
– |
|
1242 |
– |
/** |
1243 |
– |
* execute using CallerRunsPolicy drops task on shutdown |
1244 |
– |
*/ |
1245 |
– |
public void testCallerRunsOnShutdown() { |
1246 |
– |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1247 |
– |
final ThreadPoolExecutor p = |
1248 |
– |
new ThreadPoolExecutor(1, 1, |
1249 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1250 |
– |
new ArrayBlockingQueue<Runnable>(1), h); |
1251 |
– |
|
1252 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1253 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1254 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1255 |
– |
p.execute(r); |
1256 |
– |
assertFalse(r.done); |
1257 |
– |
} |
1258 |
– |
} |
1259 |
– |
|
1260 |
– |
/** |
1261 |
– |
* execute using DiscardPolicy drops task on shutdown |
1262 |
– |
*/ |
1263 |
– |
public void testDiscardOnShutdown() { |
1264 |
– |
final ThreadPoolExecutor p = |
1265 |
– |
new ThreadPoolExecutor(1, 1, |
1266 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1267 |
– |
new ArrayBlockingQueue<Runnable>(1), |
1268 |
– |
new ThreadPoolExecutor.DiscardPolicy()); |
1269 |
– |
|
1270 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1271 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1272 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1273 |
– |
p.execute(r); |
1274 |
– |
assertFalse(r.done); |
1275 |
– |
} |
1276 |
– |
} |
1277 |
– |
|
1278 |
– |
/** |
1162 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1163 |
|
*/ |
1164 |
|
public void testDiscardOldestOnShutdown() { |
1166 |
|
new ThreadPoolExecutor(1, 1, |
1167 |
|
LONG_DELAY_MS, MILLISECONDS, |
1168 |
|
new ArrayBlockingQueue<Runnable>(1), |
1169 |
< |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1169 |
> |
new DiscardOldestPolicy()); |
1170 |
|
|
1171 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1172 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1177 |
|
} |
1178 |
|
|
1179 |
|
/** |
1180 |
< |
* execute(null) throws NPE |
1180 |
> |
* Submitting null tasks throws NullPointerException |
1181 |
|
*/ |
1182 |
< |
public void testExecuteNull() { |
1182 |
> |
public void testNullTaskSubmission() { |
1183 |
|
final ThreadPoolExecutor p = |
1184 |
|
new ThreadPoolExecutor(1, 2, |
1185 |
|
1L, SECONDS, |
1186 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1187 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1188 |
< |
try { |
1306 |
< |
p.execute(null); |
1307 |
< |
shouldThrow(); |
1308 |
< |
} catch (NullPointerException success) {} |
1188 |
> |
assertNullTaskSubmissionThrowsNullPointerException(p); |
1189 |
|
} |
1190 |
|
} |
1191 |
|
|
1774 |
|
public void testTimedInvokeAll6() throws Exception { |
1775 |
|
for (long timeout = timeoutMillis();;) { |
1776 |
|
final CountDownLatch done = new CountDownLatch(1); |
1777 |
< |
final Callable<String> waiter = new CheckedCallable<String>() { |
1777 |
> |
final Callable<String> waiter = new CheckedCallable<>() { |
1778 |
|
public String realCall() { |
1779 |
|
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1780 |
|
catch (InterruptedException ok) {} |
1793 |
|
p.invokeAll(tasks, timeout, MILLISECONDS); |
1794 |
|
assertEquals(tasks.size(), futures.size()); |
1795 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1796 |
< |
for (Future future : futures) |
1796 |
> |
for (Future<?> future : futures) |
1797 |
|
assertTrue(future.isDone()); |
1798 |
|
assertTrue(futures.get(1).isCancelled()); |
1799 |
|
try { |
1908 |
|
final ThreadPoolExecutor p = |
1909 |
|
new ThreadPoolExecutor(1, 30, |
1910 |
|
60, SECONDS, |
1911 |
< |
new ArrayBlockingQueue(30)); |
1911 |
> |
new ArrayBlockingQueue<Runnable>(30)); |
1912 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1913 |
|
for (int i = 0; i < nTasks; ++i) { |
1914 |
|
for (;;) { |
1961 |
|
} |
1962 |
|
} |
1963 |
|
|
1964 |
< |
public void testAbortPolicy() { |
1965 |
< |
final RejectedExecutionHandler handler = |
2086 |
< |
new ThreadPoolExecutor.AbortPolicy(); |
1964 |
> |
/** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */ |
1965 |
> |
public void testStandardRejectedExecutionHandlers() { |
1966 |
|
final ThreadPoolExecutor p = |
1967 |
< |
new ThreadPoolExecutor(1, 1, |
1968 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1969 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1970 |
< |
final TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1967 |
> |
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
1968 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1969 |
> |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
1970 |
> |
final Runnable r = new Runnable() { public void run() { |
1971 |
> |
thread.set(Thread.currentThread()); }}; |
1972 |
> |
|
1973 |
|
try { |
1974 |
< |
handler.rejectedExecution(r, p); |
1974 |
> |
new AbortPolicy().rejectedExecution(r, p); |
1975 |
|
shouldThrow(); |
1976 |
|
} catch (RejectedExecutionException success) {} |
1977 |
< |
assertFalse(r.done); |
1977 |
> |
assertNull(thread.get()); |
1978 |
> |
|
1979 |
> |
new DiscardPolicy().rejectedExecution(r, p); |
1980 |
> |
assertNull(thread.get()); |
1981 |
> |
|
1982 |
> |
new CallerRunsPolicy().rejectedExecution(r, p); |
1983 |
> |
assertSame(Thread.currentThread(), thread.get()); |
1984 |
> |
|
1985 |
> |
// check that pool was not perturbed by handlers |
1986 |
> |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
1987 |
|
assertEquals(0, p.getTaskCount()); |
1988 |
|
assertTrue(p.getQueue().isEmpty()); |
1989 |
|
} |
1990 |
|
|
1991 |
+ |
public void testThreadFactoryReturnsTerminatedThread_shouldThrow() { |
1992 |
+ |
if (!testImplementationDetails) |
1993 |
+ |
return; |
1994 |
+ |
|
1995 |
+ |
ThreadFactory returnsTerminatedThread = runnableIgnored -> { |
1996 |
+ |
Thread thread = new Thread(() -> {}); |
1997 |
+ |
thread.start(); |
1998 |
+ |
try { thread.join(); } |
1999 |
+ |
catch (InterruptedException ex) { throw new Error(ex); } |
2000 |
+ |
return thread; |
2001 |
+ |
}; |
2002 |
+ |
ThreadPoolExecutor p = |
2003 |
+ |
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
2004 |
+ |
new ArrayBlockingQueue<Runnable>(1), |
2005 |
+ |
returnsTerminatedThread); |
2006 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
2007 |
+ |
assertThrows(IllegalThreadStateException.class, |
2008 |
+ |
() -> p.execute(() -> {})); |
2009 |
+ |
} |
2010 |
+ |
} |
2011 |
+ |
|
2012 |
+ |
public void testThreadFactoryReturnsStartedThread_shouldThrow() { |
2013 |
+ |
if (!testImplementationDetails) |
2014 |
+ |
return; |
2015 |
+ |
|
2016 |
+ |
CountDownLatch latch = new CountDownLatch(1); |
2017 |
+ |
Runnable awaitLatch = () -> { |
2018 |
+ |
try { latch.await(); } |
2019 |
+ |
catch (InterruptedException ex) { throw new Error(ex); }}; |
2020 |
+ |
ThreadFactory returnsStartedThread = runnable -> { |
2021 |
+ |
Thread thread = new Thread(awaitLatch); |
2022 |
+ |
thread.start(); |
2023 |
+ |
return thread; |
2024 |
+ |
}; |
2025 |
+ |
ThreadPoolExecutor p = |
2026 |
+ |
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
2027 |
+ |
new ArrayBlockingQueue<Runnable>(1), |
2028 |
+ |
returnsStartedThread); |
2029 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
2030 |
+ |
assertThrows(IllegalThreadStateException.class, |
2031 |
+ |
() -> p.execute(() -> {})); |
2032 |
+ |
latch.countDown(); |
2033 |
+ |
} |
2034 |
+ |
} |
2035 |
+ |
|
2036 |
|
} |