11 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
|
13 |
|
import java.util.ArrayList; |
14 |
+ |
import java.util.Collection; |
15 |
+ |
import java.util.Collections; |
16 |
|
import java.util.List; |
17 |
|
import java.util.concurrent.ArrayBlockingQueue; |
18 |
|
import java.util.concurrent.BlockingQueue; |
20 |
|
import java.util.concurrent.CancellationException; |
21 |
|
import java.util.concurrent.CountDownLatch; |
22 |
|
import java.util.concurrent.ExecutionException; |
21 |
– |
import java.util.concurrent.Executors; |
23 |
|
import java.util.concurrent.ExecutorService; |
24 |
|
import java.util.concurrent.Future; |
25 |
|
import java.util.concurrent.FutureTask; |
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.TimeUnit; |
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; |
98 |
|
final Runnable task = new CheckedRunnable() { |
99 |
|
public void realRun() { done.countDown(); }}; |
100 |
|
p.execute(task); |
101 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
101 |
> |
await(done); |
102 |
|
} |
103 |
|
} |
104 |
|
|
107 |
|
* thread becomes active |
108 |
|
*/ |
109 |
|
public void testGetActiveCount() throws InterruptedException { |
110 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
111 |
|
final ThreadPoolExecutor p = |
112 |
|
new ThreadPoolExecutor(2, 2, |
113 |
|
LONG_DELAY_MS, MILLISECONDS, |
114 |
|
new ArrayBlockingQueue<Runnable>(10)); |
115 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
115 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
116 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
– |
final CountDownLatch done = new CountDownLatch(1); |
117 |
|
assertEquals(0, p.getActiveCount()); |
118 |
|
p.execute(new CheckedRunnable() { |
119 |
|
public void realRun() throws InterruptedException { |
123 |
|
}}); |
124 |
|
await(threadStarted); |
125 |
|
assertEquals(1, p.getActiveCount()); |
120 |
– |
done.countDown(); |
126 |
|
} |
127 |
|
} |
128 |
|
|
192 |
|
public void realRun() throws InterruptedException { |
193 |
|
threadStarted.countDown(); |
194 |
|
assertEquals(0, p.getCompletedTaskCount()); |
195 |
< |
threadProceed.await(); |
195 |
> |
await(threadProceed); |
196 |
|
threadDone.countDown(); |
197 |
|
}}); |
198 |
|
await(threadStarted); |
199 |
|
assertEquals(0, p.getCompletedTaskCount()); |
200 |
|
threadProceed.countDown(); |
201 |
< |
threadDone.await(); |
201 |
> |
await(threadDone); |
202 |
|
long startTime = System.nanoTime(); |
203 |
|
while (p.getCompletedTaskCount() != 1) { |
204 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
+ |
* The default rejected execution handler is AbortPolicy. |
286 |
+ |
*/ |
287 |
+ |
public void testDefaultRejectedExecutionHandler() { |
288 |
+ |
final ThreadPoolExecutor p = |
289 |
+ |
new ThreadPoolExecutor(1, 2, |
290 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
291 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
292 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
293 |
+ |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
294 |
+ |
} |
295 |
+ |
} |
296 |
+ |
|
297 |
+ |
/** |
298 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
299 |
|
*/ |
300 |
|
public void testGetRejectedExecutionHandler() { |
477 |
|
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
478 |
|
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
479 |
|
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
480 |
< |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
481 |
< |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
480 |
> |
assertFalse(p.awaitTermination(randomExpiredTimeout(), |
481 |
> |
randomTimeUnit())); |
482 |
|
long timeoutNanos = 999999L; |
483 |
|
long startTime = System.nanoTime(); |
484 |
|
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
555 |
|
* getQueue returns the work queue, which contains queued tasks |
556 |
|
*/ |
557 |
|
public void testGetQueue() throws InterruptedException { |
558 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
558 |
> |
final CountDownLatch done = new CountDownLatch(1); |
559 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
560 |
|
final ThreadPoolExecutor p = |
561 |
|
new ThreadPoolExecutor(1, 1, |
562 |
|
LONG_DELAY_MS, MILLISECONDS, |
563 |
|
q); |
564 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
564 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
565 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
547 |
– |
final CountDownLatch done = new CountDownLatch(1); |
566 |
|
FutureTask[] tasks = new FutureTask[5]; |
567 |
|
for (int i = 0; i < tasks.length; i++) { |
568 |
|
Callable task = new CheckedCallable<Boolean>() { |
580 |
|
assertFalse(q.contains(tasks[0])); |
581 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
582 |
|
assertEquals(tasks.length - 1, q.size()); |
565 |
– |
done.countDown(); |
583 |
|
} |
584 |
|
} |
585 |
|
|
587 |
|
* remove(task) removes queued task, and fails to remove active task |
588 |
|
*/ |
589 |
|
public void testRemove() throws InterruptedException { |
590 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
590 |
> |
final CountDownLatch done = new CountDownLatch(1); |
591 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
592 |
|
final ThreadPoolExecutor p = |
593 |
|
new ThreadPoolExecutor(1, 1, |
594 |
|
LONG_DELAY_MS, MILLISECONDS, |
595 |
|
q); |
596 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
596 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
597 |
|
Runnable[] tasks = new Runnable[6]; |
598 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
581 |
– |
final CountDownLatch done = new CountDownLatch(1); |
599 |
|
for (int i = 0; i < tasks.length; i++) { |
600 |
|
tasks[i] = new CheckedRunnable() { |
601 |
|
public void realRun() throws InterruptedException { |
614 |
|
assertTrue(q.contains(tasks[3])); |
615 |
|
assertTrue(p.remove(tasks[3])); |
616 |
|
assertFalse(q.contains(tasks[3])); |
600 |
– |
done.countDown(); |
617 |
|
} |
618 |
|
} |
619 |
|
|
623 |
|
public void testPurge() throws InterruptedException { |
624 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
625 |
|
final CountDownLatch done = new CountDownLatch(1); |
626 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
626 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
627 |
|
final ThreadPoolExecutor p = |
628 |
|
new ThreadPoolExecutor(1, 1, |
629 |
|
LONG_DELAY_MS, MILLISECONDS, |
1059 |
|
p.submit(task).get(); |
1060 |
|
}}); |
1061 |
|
|
1062 |
< |
await(threadStarted); |
1062 |
> |
await(threadStarted); // ensure quiescence |
1063 |
|
t.interrupt(); |
1064 |
|
awaitTermination(t); |
1065 |
|
} |
1066 |
|
} |
1067 |
|
|
1068 |
|
/** |
1069 |
< |
* execute throws RejectedExecutionException if saturated. |
1069 |
> |
* Submitted tasks are rejected when saturated or shutdown |
1070 |
|
*/ |
1071 |
< |
public void testSaturatedExecute() { |
1072 |
< |
final ThreadPoolExecutor p = |
1073 |
< |
new ThreadPoolExecutor(1, 1, |
1074 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1075 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
1076 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1077 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1078 |
< |
Runnable task = new CheckedRunnable() { |
1079 |
< |
public void realRun() throws InterruptedException { |
1080 |
< |
await(done); |
1065 |
< |
}}; |
1066 |
< |
for (int i = 0; i < 2; ++i) |
1067 |
< |
p.execute(task); |
1068 |
< |
for (int i = 0; i < 2; ++i) { |
1071 |
> |
public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException { |
1072 |
> |
final ThreadPoolExecutor p = new ThreadPoolExecutor( |
1073 |
> |
1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1074 |
> |
final int saturatedSize = saturatedSize(p); |
1075 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1076 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize()); |
1077 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1078 |
> |
final Runnable r = () -> { |
1079 |
> |
threadsStarted.countDown(); |
1080 |
> |
for (;;) { |
1081 |
|
try { |
1082 |
< |
p.execute(task); |
1083 |
< |
shouldThrow(); |
1084 |
< |
} catch (RejectedExecutionException success) {} |
1085 |
< |
assertTrue(p.getTaskCount() <= 2); |
1086 |
< |
} |
1087 |
< |
done.countDown(); |
1088 |
< |
} |
1077 |
< |
} |
1078 |
< |
|
1079 |
< |
/** |
1080 |
< |
* submit(runnable) throws RejectedExecutionException if saturated. |
1081 |
< |
*/ |
1082 |
< |
public void testSaturatedSubmitRunnable() { |
1083 |
< |
final ThreadPoolExecutor p = |
1084 |
< |
new ThreadPoolExecutor(1, 1, |
1085 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1086 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
1087 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1088 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1089 |
< |
Runnable task = new CheckedRunnable() { |
1090 |
< |
public void realRun() throws InterruptedException { |
1091 |
< |
await(done); |
1092 |
< |
}}; |
1093 |
< |
for (int i = 0; i < 2; ++i) |
1094 |
< |
p.submit(task); |
1095 |
< |
for (int i = 0; i < 2; ++i) { |
1082 |
> |
done.await(); |
1083 |
> |
return; |
1084 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1085 |
> |
}}; |
1086 |
> |
final Callable<Boolean> c = () -> { |
1087 |
> |
threadsStarted.countDown(); |
1088 |
> |
for (;;) { |
1089 |
|
try { |
1090 |
< |
p.execute(task); |
1091 |
< |
shouldThrow(); |
1092 |
< |
} catch (RejectedExecutionException success) {} |
1093 |
< |
assertTrue(p.getTaskCount() <= 2); |
1094 |
< |
} |
1102 |
< |
done.countDown(); |
1103 |
< |
} |
1104 |
< |
} |
1090 |
> |
done.await(); |
1091 |
> |
return Boolean.TRUE; |
1092 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1093 |
> |
}}; |
1094 |
> |
final boolean shutdownNow = rnd.nextBoolean(); |
1095 |
|
|
1096 |
< |
/** |
1097 |
< |
* submit(callable) throws RejectedExecutionException if saturated. |
1098 |
< |
*/ |
1099 |
< |
public void testSaturatedSubmitCallable() { |
1100 |
< |
final ThreadPoolExecutor p = |
1101 |
< |
new ThreadPoolExecutor(1, 1, |
1102 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1103 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
1104 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1115 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1116 |
< |
Runnable task = new CheckedRunnable() { |
1117 |
< |
public void realRun() throws InterruptedException { |
1118 |
< |
await(done); |
1119 |
< |
}}; |
1120 |
< |
for (int i = 0; i < 2; ++i) |
1121 |
< |
p.submit(Executors.callable(task)); |
1122 |
< |
for (int i = 0; i < 2; ++i) { |
1123 |
< |
try { |
1124 |
< |
p.execute(task); |
1125 |
< |
shouldThrow(); |
1126 |
< |
} catch (RejectedExecutionException success) {} |
1127 |
< |
assertTrue(p.getTaskCount() <= 2); |
1096 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1097 |
> |
// saturate |
1098 |
> |
for (int i = saturatedSize; i--> 0; ) { |
1099 |
> |
switch (rnd.nextInt(4)) { |
1100 |
> |
case 0: p.execute(r); break; |
1101 |
> |
case 1: assertFalse(p.submit(r).isDone()); break; |
1102 |
> |
case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; |
1103 |
> |
case 3: assertFalse(p.submit(c).isDone()); break; |
1104 |
> |
} |
1105 |
|
} |
1129 |
– |
done.countDown(); |
1130 |
– |
} |
1131 |
– |
} |
1106 |
|
|
1107 |
< |
/** |
1108 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1135 |
< |
*/ |
1136 |
< |
public void testSaturatedExecute2() { |
1137 |
< |
final ThreadPoolExecutor p = |
1138 |
< |
new ThreadPoolExecutor(1, 1, |
1139 |
< |
LONG_DELAY_MS, |
1140 |
< |
MILLISECONDS, |
1141 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1142 |
< |
new ThreadPoolExecutor.CallerRunsPolicy()); |
1143 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1144 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1145 |
< |
Runnable blocker = new CheckedRunnable() { |
1146 |
< |
public void realRun() throws InterruptedException { |
1147 |
< |
await(done); |
1148 |
< |
}}; |
1149 |
< |
p.execute(blocker); |
1150 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1151 |
< |
for (int i = 0; i < tasks.length; i++) |
1152 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1153 |
< |
for (int i = 0; i < tasks.length; i++) |
1154 |
< |
p.execute(tasks[i]); |
1155 |
< |
for (int i = 1; i < tasks.length; i++) |
1156 |
< |
assertTrue(tasks[i].done); |
1157 |
< |
assertFalse(tasks[0].done); // waiting in queue |
1158 |
< |
done.countDown(); |
1159 |
< |
} |
1160 |
< |
} |
1107 |
> |
await(threadsStarted); |
1108 |
> |
assertTaskSubmissionsAreRejected(p); |
1109 |
|
|
1110 |
< |
/** |
1111 |
< |
* executor using DiscardPolicy drops task if saturated. |
1112 |
< |
*/ |
1113 |
< |
public void testSaturatedExecute3() { |
1114 |
< |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1115 |
< |
for (int i = 0; i < tasks.length; ++i) |
1116 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1169 |
< |
final ThreadPoolExecutor p = |
1170 |
< |
new ThreadPoolExecutor(1, 1, |
1171 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1172 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1173 |
< |
new ThreadPoolExecutor.DiscardPolicy()); |
1174 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1175 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1176 |
< |
p.execute(awaiter(done)); |
1110 |
> |
if (shutdownNow) |
1111 |
> |
p.shutdownNow(); |
1112 |
> |
else |
1113 |
> |
p.shutdown(); |
1114 |
> |
// Pool is shutdown, but not yet terminated |
1115 |
> |
assertTaskSubmissionsAreRejected(p); |
1116 |
> |
assertFalse(p.isTerminated()); |
1117 |
|
|
1118 |
< |
for (TrackedNoOpRunnable task : tasks) |
1119 |
< |
p.execute(task); |
1120 |
< |
for (int i = 1; i < tasks.length; i++) |
1121 |
< |
assertFalse(tasks[i].done); |
1182 |
< |
done.countDown(); |
1118 |
> |
done.countDown(); // release blocking tasks |
1119 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
1120 |
> |
|
1121 |
> |
assertTaskSubmissionsAreRejected(p); |
1122 |
|
} |
1123 |
< |
for (int i = 1; i < tasks.length; i++) |
1124 |
< |
assertFalse(tasks[i].done); |
1125 |
< |
assertTrue(tasks[0].done); // was waiting in queue |
1123 |
> |
assertEquals(saturatedSize(p) |
1124 |
> |
- (shutdownNow ? p.getQueue().remainingCapacity() : 0), |
1125 |
> |
p.getCompletedTaskCount()); |
1126 |
|
} |
1127 |
|
|
1128 |
|
/** |
1129 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1130 |
|
*/ |
1131 |
< |
public void testSaturatedExecute4() { |
1131 |
> |
public void testSaturatedExecute_DiscardOldestPolicy() { |
1132 |
|
final CountDownLatch done = new CountDownLatch(1); |
1133 |
|
LatchAwaiter r1 = awaiter(done); |
1134 |
|
LatchAwaiter r2 = awaiter(done); |
1137 |
|
new ThreadPoolExecutor(1, 1, |
1138 |
|
LONG_DELAY_MS, MILLISECONDS, |
1139 |
|
new ArrayBlockingQueue<Runnable>(1), |
1140 |
< |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1141 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1140 |
> |
new DiscardOldestPolicy()); |
1141 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1142 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1143 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1144 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1148 |
|
p.execute(r3); |
1149 |
|
assertFalse(p.getQueue().contains(r2)); |
1150 |
|
assertTrue(p.getQueue().contains(r3)); |
1212 |
– |
done.countDown(); |
1151 |
|
} |
1152 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1153 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1155 |
|
} |
1156 |
|
|
1157 |
|
/** |
1220 |
– |
* execute throws RejectedExecutionException if shutdown |
1221 |
– |
*/ |
1222 |
– |
public void testRejectedExecutionExceptionOnShutdown() { |
1223 |
– |
final ThreadPoolExecutor p = |
1224 |
– |
new ThreadPoolExecutor(1, 1, |
1225 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1226 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1227 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1228 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1229 |
– |
try { |
1230 |
– |
p.execute(new NoOpRunnable()); |
1231 |
– |
shouldThrow(); |
1232 |
– |
} catch (RejectedExecutionException success) {} |
1233 |
– |
} |
1234 |
– |
} |
1235 |
– |
|
1236 |
– |
/** |
1237 |
– |
* execute using CallerRunsPolicy drops task on shutdown |
1238 |
– |
*/ |
1239 |
– |
public void testCallerRunsOnShutdown() { |
1240 |
– |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1241 |
– |
final ThreadPoolExecutor p = |
1242 |
– |
new ThreadPoolExecutor(1, 1, |
1243 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1244 |
– |
new ArrayBlockingQueue<Runnable>(1), h); |
1245 |
– |
|
1246 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1247 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1248 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1249 |
– |
p.execute(r); |
1250 |
– |
assertFalse(r.done); |
1251 |
– |
} |
1252 |
– |
} |
1253 |
– |
|
1254 |
– |
/** |
1255 |
– |
* execute using DiscardPolicy drops task on shutdown |
1256 |
– |
*/ |
1257 |
– |
public void testDiscardOnShutdown() { |
1258 |
– |
final ThreadPoolExecutor p = |
1259 |
– |
new ThreadPoolExecutor(1, 1, |
1260 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1261 |
– |
new ArrayBlockingQueue<Runnable>(1), |
1262 |
– |
new ThreadPoolExecutor.DiscardPolicy()); |
1263 |
– |
|
1264 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1265 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1266 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1267 |
– |
p.execute(r); |
1268 |
– |
assertFalse(r.done); |
1269 |
– |
} |
1270 |
– |
} |
1271 |
– |
|
1272 |
– |
/** |
1158 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1159 |
|
*/ |
1160 |
|
public void testDiscardOldestOnShutdown() { |
1162 |
|
new ThreadPoolExecutor(1, 1, |
1163 |
|
LONG_DELAY_MS, MILLISECONDS, |
1164 |
|
new ArrayBlockingQueue<Runnable>(1), |
1165 |
< |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1165 |
> |
new DiscardOldestPolicy()); |
1166 |
|
|
1167 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1168 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1173 |
|
} |
1174 |
|
|
1175 |
|
/** |
1176 |
< |
* execute(null) throws NPE |
1176 |
> |
* Submitting null tasks throws NullPointerException |
1177 |
|
*/ |
1178 |
< |
public void testExecuteNull() { |
1178 |
> |
public void testNullTaskSubmission() { |
1179 |
|
final ThreadPoolExecutor p = |
1180 |
|
new ThreadPoolExecutor(1, 2, |
1181 |
|
1L, SECONDS, |
1182 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1183 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1184 |
< |
try { |
1300 |
< |
p.execute(null); |
1301 |
< |
shouldThrow(); |
1302 |
< |
} catch (NullPointerException success) {} |
1184 |
> |
assertNullTaskSubmissionThrowsNullPointerException(p); |
1185 |
|
} |
1186 |
|
} |
1187 |
|
|
1373 |
|
} |
1374 |
|
|
1375 |
|
/** |
1376 |
< |
* invokeAny(empty collection) throws IAE |
1376 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
1377 |
|
*/ |
1378 |
|
public void testInvokeAny2() throws Exception { |
1379 |
|
final ExecutorService e = |
1398 |
|
LONG_DELAY_MS, MILLISECONDS, |
1399 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1400 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1401 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1401 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1402 |
|
l.add(latchAwaitingStringTask(latch)); |
1403 |
|
l.add(null); |
1404 |
|
try { |
1418 |
|
LONG_DELAY_MS, MILLISECONDS, |
1419 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1420 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1421 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1421 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1422 |
|
l.add(new NPETask()); |
1423 |
|
try { |
1424 |
|
e.invokeAny(l); |
1438 |
|
LONG_DELAY_MS, MILLISECONDS, |
1439 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1440 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1441 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1441 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1442 |
|
l.add(new StringTask()); |
1443 |
|
l.add(new StringTask()); |
1444 |
|
String result = e.invokeAny(l); |
1463 |
|
} |
1464 |
|
|
1465 |
|
/** |
1466 |
< |
* invokeAll(empty collection) returns empty collection |
1466 |
> |
* invokeAll(empty collection) returns empty list |
1467 |
|
*/ |
1468 |
|
public void testInvokeAll2() throws InterruptedException { |
1469 |
|
final ExecutorService e = |
1470 |
|
new ThreadPoolExecutor(2, 2, |
1471 |
|
LONG_DELAY_MS, MILLISECONDS, |
1472 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1473 |
+ |
final Collection<Callable<String>> emptyCollection |
1474 |
+ |
= Collections.emptyList(); |
1475 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1476 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1476 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1477 |
|
assertTrue(r.isEmpty()); |
1478 |
|
} |
1479 |
|
} |
1487 |
|
LONG_DELAY_MS, MILLISECONDS, |
1488 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1489 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1490 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1490 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1491 |
|
l.add(new StringTask()); |
1492 |
|
l.add(null); |
1493 |
|
try { |
1506 |
|
LONG_DELAY_MS, MILLISECONDS, |
1507 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1508 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1509 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1509 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1510 |
|
l.add(new NPETask()); |
1511 |
|
List<Future<String>> futures = e.invokeAll(l); |
1512 |
|
assertEquals(1, futures.size()); |
1528 |
|
LONG_DELAY_MS, MILLISECONDS, |
1529 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1530 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1531 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1531 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1532 |
|
l.add(new StringTask()); |
1533 |
|
l.add(new StringTask()); |
1534 |
|
List<Future<String>> futures = e.invokeAll(l); |
1548 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1549 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1550 |
|
try { |
1551 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1551 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1552 |
|
shouldThrow(); |
1553 |
|
} catch (NullPointerException success) {} |
1554 |
|
} |
1563 |
|
LONG_DELAY_MS, MILLISECONDS, |
1564 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1565 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1566 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1566 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1567 |
|
l.add(new StringTask()); |
1568 |
|
try { |
1569 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1569 |
> |
e.invokeAny(l, randomTimeout(), null); |
1570 |
|
shouldThrow(); |
1571 |
|
} catch (NullPointerException success) {} |
1572 |
|
} |
1573 |
|
} |
1574 |
|
|
1575 |
|
/** |
1576 |
< |
* timed invokeAny(empty collection) throws IAE |
1576 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1577 |
|
*/ |
1578 |
|
public void testTimedInvokeAny2() throws Exception { |
1579 |
|
final ExecutorService e = |
1583 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1584 |
|
try { |
1585 |
|
e.invokeAny(new ArrayList<Callable<String>>(), |
1586 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1586 |
> |
randomTimeout(), randomTimeUnit()); |
1587 |
|
shouldThrow(); |
1588 |
|
} catch (IllegalArgumentException success) {} |
1589 |
|
} |
1590 |
|
} |
1591 |
|
|
1592 |
|
/** |
1593 |
< |
* timed invokeAny(c) throws NPE if c has null elements |
1593 |
> |
* timed invokeAny(c) throws NullPointerException if c has null elements |
1594 |
|
*/ |
1595 |
|
public void testTimedInvokeAny3() throws Exception { |
1596 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1599 |
|
LONG_DELAY_MS, MILLISECONDS, |
1600 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1601 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1602 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1602 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1603 |
|
l.add(latchAwaitingStringTask(latch)); |
1604 |
|
l.add(null); |
1605 |
|
try { |
1606 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1606 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
1607 |
|
shouldThrow(); |
1608 |
|
} catch (NullPointerException success) {} |
1609 |
|
latch.countDown(); |
1619 |
|
LONG_DELAY_MS, MILLISECONDS, |
1620 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1621 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1622 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1622 |
> |
long startTime = System.nanoTime(); |
1623 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1624 |
|
l.add(new NPETask()); |
1625 |
|
try { |
1626 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1626 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1627 |
|
shouldThrow(); |
1628 |
|
} catch (ExecutionException success) { |
1629 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1630 |
|
} |
1631 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1632 |
|
} |
1633 |
|
} |
1634 |
|
|
1641 |
|
LONG_DELAY_MS, MILLISECONDS, |
1642 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1643 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1644 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1644 |
> |
long startTime = System.nanoTime(); |
1645 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1646 |
|
l.add(new StringTask()); |
1647 |
|
l.add(new StringTask()); |
1648 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1648 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1649 |
|
assertSame(TEST_STRING, result); |
1650 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1651 |
|
} |
1652 |
|
} |
1653 |
|
|
1661 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1662 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1663 |
|
try { |
1664 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1664 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1665 |
|
shouldThrow(); |
1666 |
|
} catch (NullPointerException success) {} |
1667 |
|
} |
1676 |
|
LONG_DELAY_MS, MILLISECONDS, |
1677 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1678 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1679 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1679 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1680 |
|
l.add(new StringTask()); |
1681 |
|
try { |
1682 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1682 |
> |
e.invokeAll(l, randomTimeout(), null); |
1683 |
|
shouldThrow(); |
1684 |
|
} catch (NullPointerException success) {} |
1685 |
|
} |
1686 |
|
} |
1687 |
|
|
1688 |
|
/** |
1689 |
< |
* timed invokeAll(empty collection) returns empty collection |
1689 |
> |
* timed invokeAll(empty collection) returns empty list |
1690 |
|
*/ |
1691 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1692 |
|
final ExecutorService e = |
1693 |
|
new ThreadPoolExecutor(2, 2, |
1694 |
|
LONG_DELAY_MS, MILLISECONDS, |
1695 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1696 |
+ |
final Collection<Callable<String>> emptyCollection |
1697 |
+ |
= Collections.emptyList(); |
1698 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1699 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1700 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1699 |
> |
List<Future<String>> r = |
1700 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1701 |
|
assertTrue(r.isEmpty()); |
1702 |
|
} |
1703 |
|
} |
1711 |
|
LONG_DELAY_MS, MILLISECONDS, |
1712 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1713 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1714 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1714 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1715 |
|
l.add(new StringTask()); |
1716 |
|
l.add(null); |
1717 |
|
try { |
1718 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1718 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1719 |
|
shouldThrow(); |
1720 |
|
} catch (NullPointerException success) {} |
1721 |
|
} |
1730 |
|
LONG_DELAY_MS, MILLISECONDS, |
1731 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1732 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1733 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1733 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1734 |
|
l.add(new NPETask()); |
1735 |
|
List<Future<String>> futures = |
1736 |
|
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1753 |
|
LONG_DELAY_MS, MILLISECONDS, |
1754 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1755 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1756 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1756 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1757 |
|
l.add(new StringTask()); |
1758 |
|
l.add(new StringTask()); |
1759 |
|
List<Future<String>> futures = |
1768 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1769 |
|
*/ |
1770 |
|
public void testTimedInvokeAll6() throws Exception { |
1771 |
< |
final ExecutorService e = |
1772 |
< |
new ThreadPoolExecutor(2, 2, |
1773 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1774 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1775 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1776 |
< |
for (long timeout = timeoutMillis();;) { |
1771 |
> |
for (long timeout = timeoutMillis();;) { |
1772 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1773 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1774 |
> |
public String realCall() { |
1775 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1776 |
> |
catch (InterruptedException ok) {} |
1777 |
> |
return "1"; }}; |
1778 |
> |
final ExecutorService p = |
1779 |
> |
new ThreadPoolExecutor(2, 2, |
1780 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1781 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1782 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1783 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1784 |
|
tasks.add(new StringTask("0")); |
1785 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1785 |
> |
tasks.add(waiter); |
1786 |
|
tasks.add(new StringTask("2")); |
1787 |
|
long startTime = System.nanoTime(); |
1788 |
|
List<Future<String>> futures = |
1789 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1789 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1790 |
|
assertEquals(tasks.size(), futures.size()); |
1791 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1792 |
|
for (Future future : futures) |
1823 |
|
public void realRun() { |
1824 |
|
done.countDown(); |
1825 |
|
}}); |
1826 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1826 |
> |
await(done); |
1827 |
|
} |
1828 |
|
} |
1829 |
|
|
1916 |
|
} |
1917 |
|
} |
1918 |
|
// enough time to run all tasks |
1919 |
< |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
1919 |
> |
await(done, nTasks * SHORT_DELAY_MS); |
1920 |
|
} |
1921 |
|
} |
1922 |
|
|
1924 |
|
* get(cancelled task) throws CancellationException |
1925 |
|
*/ |
1926 |
|
public void testGet_cancelled() throws Exception { |
1927 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1928 |
|
final ExecutorService e = |
1929 |
|
new ThreadPoolExecutor(1, 1, |
1930 |
|
LONG_DELAY_MS, MILLISECONDS, |
1931 |
|
new LinkedBlockingQueue<Runnable>()); |
1932 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1932 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
1933 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2037 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1934 |
|
final List<Future<?>> futures = new ArrayList<>(); |
1935 |
|
for (int i = 0; i < 2; i++) { |
1936 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
1954 |
|
assertTrue(future.isCancelled()); |
1955 |
|
assertTrue(future.isDone()); |
1956 |
|
} |
2061 |
– |
done.countDown(); |
1957 |
|
} |
1958 |
|
} |
1959 |
|
|
1960 |
+ |
/** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */ |
1961 |
+ |
public void testStandardRejectedExecutionHandlers() { |
1962 |
+ |
final ThreadPoolExecutor p = |
1963 |
+ |
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
1964 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
1965 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
1966 |
+ |
final Runnable r = new Runnable() { public void run() { |
1967 |
+ |
thread.set(Thread.currentThread()); }}; |
1968 |
+ |
|
1969 |
+ |
try { |
1970 |
+ |
new AbortPolicy().rejectedExecution(r, p); |
1971 |
+ |
shouldThrow(); |
1972 |
+ |
} catch (RejectedExecutionException success) {} |
1973 |
+ |
assertNull(thread.get()); |
1974 |
+ |
|
1975 |
+ |
new DiscardPolicy().rejectedExecution(r, p); |
1976 |
+ |
assertNull(thread.get()); |
1977 |
+ |
|
1978 |
+ |
new CallerRunsPolicy().rejectedExecution(r, p); |
1979 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
1980 |
+ |
|
1981 |
+ |
// check that pool was not perturbed by handlers |
1982 |
+ |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
1983 |
+ |
assertEquals(0, p.getTaskCount()); |
1984 |
+ |
assertTrue(p.getQueue().isEmpty()); |
1985 |
+ |
} |
1986 |
+ |
|
1987 |
|
} |