1206 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1207 |
|
*/ |
1208 |
|
public void testSaturatedExecute4() { |
1209 |
< |
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1210 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1211 |
< |
try { |
1212 |
< |
p.execute(new TrackedLongRunnable()); |
1213 |
< |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1209 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1210 |
> |
LatchAwaiter r1 = awaiter(done); |
1211 |
> |
LatchAwaiter r2 = awaiter(done); |
1212 |
> |
LatchAwaiter r3 = awaiter(done); |
1213 |
> |
final ThreadPoolExecutor p = |
1214 |
> |
new CustomTPE(1, 1, |
1215 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1216 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1217 |
> |
new CustomTPE.DiscardOldestPolicy()); |
1218 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1219 |
> |
assertEquals(LatchAwaiter.NEW, r1.state); |
1220 |
> |
assertEquals(LatchAwaiter.NEW, r2.state); |
1221 |
> |
assertEquals(LatchAwaiter.NEW, r3.state); |
1222 |
> |
p.execute(r1); |
1223 |
|
p.execute(r2); |
1224 |
|
assertTrue(p.getQueue().contains(r2)); |
1216 |
– |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1225 |
|
p.execute(r3); |
1226 |
|
assertFalse(p.getQueue().contains(r2)); |
1227 |
|
assertTrue(p.getQueue().contains(r3)); |
1228 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1221 |
< |
} finally { |
1222 |
< |
joinPool(p); |
1228 |
> |
done.countDown(); |
1229 |
|
} |
1230 |
+ |
assertEquals(LatchAwaiter.DONE, r1.state); |
1231 |
+ |
assertEquals(LatchAwaiter.NEW, r2.state); |
1232 |
+ |
assertEquals(LatchAwaiter.DONE, r3.state); |
1233 |
|
} |
1234 |
|
|
1235 |
|
/** |
1236 |
|
* execute throws RejectedExecutionException if shutdown |
1237 |
|
*/ |
1238 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1239 |
< |
ThreadPoolExecutor p = |
1240 |
< |
new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1239 |
> |
final ThreadPoolExecutor p = |
1240 |
> |
new CustomTPE(1, 1, |
1241 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1242 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1243 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1244 |
< |
try { |
1245 |
< |
p.execute(new NoOpRunnable()); |
1246 |
< |
shouldThrow(); |
1247 |
< |
} catch (RejectedExecutionException success) {} |
1248 |
< |
|
1249 |
< |
joinPool(p); |
1244 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1245 |
> |
try { |
1246 |
> |
p.execute(new NoOpRunnable()); |
1247 |
> |
shouldThrow(); |
1248 |
> |
} catch (RejectedExecutionException success) {} |
1249 |
> |
} |
1250 |
|
} |
1251 |
|
|
1252 |
|
/** |
1257 |
|
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1258 |
|
|
1259 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1260 |
< |
try { |
1260 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1261 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1262 |
|
p.execute(r); |
1263 |
|
assertFalse(r.done); |
1253 |
– |
} finally { |
1254 |
– |
joinPool(p); |
1264 |
|
} |
1265 |
|
} |
1266 |
|
|