29 |
|
import java.util.concurrent.SynchronousQueue; |
30 |
|
import java.util.concurrent.ThreadFactory; |
31 |
|
import java.util.concurrent.ThreadPoolExecutor; |
32 |
+ |
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; |
33 |
+ |
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; |
34 |
+ |
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; |
35 |
+ |
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy; |
36 |
|
import java.util.concurrent.atomic.AtomicInteger; |
37 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
38 |
|
|
39 |
|
import junit.framework.Test; |
40 |
|
import junit.framework.TestSuite; |
289 |
|
LONG_DELAY_MS, MILLISECONDS, |
290 |
|
new ArrayBlockingQueue<Runnable>(10)); |
291 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
292 |
< |
assertTrue(p.getRejectedExecutionHandler() |
288 |
< |
instanceof ThreadPoolExecutor.AbortPolicy); |
292 |
> |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
293 |
|
} |
294 |
|
} |
295 |
|
|
1151 |
|
LONG_DELAY_MS, |
1152 |
|
MILLISECONDS, |
1153 |
|
new ArrayBlockingQueue<Runnable>(1), |
1154 |
< |
new ThreadPoolExecutor.CallerRunsPolicy()); |
1154 |
> |
new CallerRunsPolicy()); |
1155 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1156 |
|
final CountDownLatch done = new CountDownLatch(1); |
1157 |
|
Runnable blocker = new CheckedRunnable() { |
1183 |
|
new ThreadPoolExecutor(1, 1, |
1184 |
|
LONG_DELAY_MS, MILLISECONDS, |
1185 |
|
new ArrayBlockingQueue<Runnable>(1), |
1186 |
< |
new ThreadPoolExecutor.DiscardPolicy()); |
1186 |
> |
new DiscardPolicy()); |
1187 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1188 |
|
p.execute(awaiter(done)); |
1189 |
|
|
1209 |
|
new ThreadPoolExecutor(1, 1, |
1210 |
|
LONG_DELAY_MS, MILLISECONDS, |
1211 |
|
new ArrayBlockingQueue<Runnable>(1), |
1212 |
< |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1212 |
> |
new DiscardOldestPolicy()); |
1213 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1214 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1215 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1247 |
|
* execute using CallerRunsPolicy drops task on shutdown |
1248 |
|
*/ |
1249 |
|
public void testCallerRunsOnShutdown() { |
1250 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1250 |
> |
RejectedExecutionHandler h = new CallerRunsPolicy(); |
1251 |
|
final ThreadPoolExecutor p = |
1252 |
|
new ThreadPoolExecutor(1, 1, |
1253 |
|
LONG_DELAY_MS, MILLISECONDS, |
1269 |
|
new ThreadPoolExecutor(1, 1, |
1270 |
|
LONG_DELAY_MS, MILLISECONDS, |
1271 |
|
new ArrayBlockingQueue<Runnable>(1), |
1272 |
< |
new ThreadPoolExecutor.DiscardPolicy()); |
1272 |
> |
new DiscardPolicy()); |
1273 |
|
|
1274 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1275 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1287 |
|
new ThreadPoolExecutor(1, 1, |
1288 |
|
LONG_DELAY_MS, MILLISECONDS, |
1289 |
|
new ArrayBlockingQueue<Runnable>(1), |
1290 |
< |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1290 |
> |
new DiscardOldestPolicy()); |
1291 |
|
|
1292 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1293 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
2086 |
|
} |
2087 |
|
|
2088 |
|
public void testAbortPolicy() { |
2089 |
< |
final RejectedExecutionHandler handler = |
2086 |
< |
new ThreadPoolExecutor.AbortPolicy(); |
2089 |
> |
final RejectedExecutionHandler handler = new AbortPolicy(); |
2090 |
|
final ThreadPoolExecutor p = |
2091 |
|
new ThreadPoolExecutor(1, 1, |
2092 |
|
LONG_DELAY_MS, MILLISECONDS, |
2100 |
|
assertEquals(0, p.getTaskCount()); |
2101 |
|
assertTrue(p.getQueue().isEmpty()); |
2102 |
|
} |
2103 |
+ |
|
2104 |
+ |
public void testCallerRunsPolicy() { |
2105 |
+ |
final RejectedExecutionHandler handler = new CallerRunsPolicy(); |
2106 |
+ |
final ThreadPoolExecutor p = |
2107 |
+ |
new ThreadPoolExecutor(1, 1, |
2108 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
2109 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
2110 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2111 |
+ |
final Runnable r = new Runnable() { public void run() { |
2112 |
+ |
thread.set(Thread.currentThread()); }}; |
2113 |
+ |
handler.rejectedExecution(r, p); |
2114 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
2115 |
+ |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
2116 |
+ |
assertEquals(0, p.getTaskCount()); |
2117 |
+ |
assertTrue(p.getQueue().isEmpty()); |
2118 |
+ |
} |
2119 |
|
|
2120 |
|
} |