1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
60 |
|
finally { lock.unlock() ; } |
61 |
|
} |
62 |
|
public void run() { |
63 |
– |
boolean runme; |
63 |
|
lock.lock(); |
64 |
|
try { |
65 |
< |
runme = !done; |
66 |
< |
if (!runme) |
67 |
< |
thread = Thread.currentThread(); |
65 |
> |
if (done) |
66 |
> |
return; |
67 |
> |
thread = Thread.currentThread(); |
68 |
|
} |
69 |
|
finally { lock.unlock() ; } |
71 |
– |
if (!runme) return; |
70 |
|
V v = null; |
71 |
|
Exception e = null; |
72 |
|
try { |
115 |
|
} |
116 |
|
} |
117 |
|
|
120 |
– |
|
118 |
|
static class CustomTPE extends ThreadPoolExecutor { |
119 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
120 |
|
return new CustomTask<V>(c); |
161 |
|
workQueue, threadFactory, handler); |
162 |
|
} |
163 |
|
|
164 |
< |
volatile boolean beforeCalled = false; |
165 |
< |
volatile boolean afterCalled = false; |
166 |
< |
volatile boolean terminatedCalled = false; |
164 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
165 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
166 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
167 |
> |
|
168 |
|
public CustomTPE() { |
169 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
170 |
|
} |
171 |
|
protected void beforeExecute(Thread t, Runnable r) { |
172 |
< |
beforeCalled = true; |
172 |
> |
beforeCalled.countDown(); |
173 |
|
} |
174 |
|
protected void afterExecute(Runnable r, Throwable t) { |
175 |
< |
afterCalled = true; |
175 |
> |
afterCalled.countDown(); |
176 |
|
} |
177 |
|
protected void terminated() { |
178 |
< |
terminatedCalled = true; |
178 |
> |
terminatedCalled.countDown(); |
179 |
|
} |
180 |
|
|
181 |
+ |
public boolean beforeCalled() { |
182 |
+ |
return beforeCalled.getCount() == 0; |
183 |
+ |
} |
184 |
+ |
public boolean afterCalled() { |
185 |
+ |
return afterCalled.getCount() == 0; |
186 |
+ |
} |
187 |
+ |
public boolean terminatedCalled() { |
188 |
+ |
return terminatedCalled.getCount() == 0; |
189 |
+ |
} |
190 |
|
} |
191 |
|
|
192 |
|
static class FailingThreadFactory implements ThreadFactory { |
197 |
|
} |
198 |
|
} |
199 |
|
|
193 |
– |
|
200 |
|
/** |
201 |
|
* execute successfully executes a runnable |
202 |
|
*/ |
294 |
|
threadProceed.await(); |
295 |
|
threadDone.countDown(); |
296 |
|
}}); |
297 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
297 |
> |
await(threadStarted); |
298 |
|
assertEquals(0, p.getCompletedTaskCount()); |
299 |
|
threadProceed.countDown(); |
300 |
|
threadDone.await(); |
301 |
< |
Thread.sleep(SHORT_DELAY_MS); |
302 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
301 |
> |
long startTime = System.nanoTime(); |
302 |
> |
while (p.getCompletedTaskCount() != 1) { |
303 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
304 |
> |
fail("timed out"); |
305 |
> |
Thread.yield(); |
306 |
> |
} |
307 |
|
} finally { |
308 |
|
joinPool(p); |
309 |
|
} |
327 |
|
joinPool(p); |
328 |
|
} |
329 |
|
|
320 |
– |
|
330 |
|
/** |
331 |
|
* getThreadFactory returns factory in constructor if not set |
332 |
|
*/ |
348 |
|
joinPool(p); |
349 |
|
} |
350 |
|
|
342 |
– |
|
351 |
|
/** |
352 |
|
* setThreadFactory(null) throws NPE |
353 |
|
*/ |
384 |
|
joinPool(p); |
385 |
|
} |
386 |
|
|
379 |
– |
|
387 |
|
/** |
388 |
|
* setRejectedExecutionHandler(null) throws NPE |
389 |
|
*/ |
398 |
|
} |
399 |
|
} |
400 |
|
|
394 |
– |
|
401 |
|
/** |
402 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
403 |
|
* multiple threads active |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
< |
* isShutDown is false before shutdown, true after |
495 |
> |
* isShutdown is false before shutdown, true after |
496 |
|
*/ |
497 |
|
public void testIsShutdown() { |
498 |
|
|
503 |
|
joinPool(p); |
504 |
|
} |
505 |
|
|
500 |
– |
|
506 |
|
/** |
507 |
|
* isTerminated is false before termination, true after |
508 |
|
*/ |
517 |
|
assertFalse(p.isTerminating()); |
518 |
|
p.execute(new CheckedRunnable() { |
519 |
|
public void realRun() throws InterruptedException { |
515 |
– |
threadStarted.countDown(); |
520 |
|
assertFalse(p.isTerminating()); |
521 |
+ |
threadStarted.countDown(); |
522 |
|
done.await(); |
523 |
|
}}); |
524 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
546 |
|
assertFalse(p.isTerminating()); |
547 |
|
p.execute(new CheckedRunnable() { |
548 |
|
public void realRun() throws InterruptedException { |
544 |
– |
threadStarted.countDown(); |
549 |
|
assertFalse(p.isTerminating()); |
550 |
+ |
threadStarted.countDown(); |
551 |
|
done.await(); |
552 |
|
}}); |
553 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
679 |
< |
* shutDownNow returns a list containing tasks that were not run |
679 |
> |
* shutdownNow returns a list containing tasks that were not run |
680 |
|
*/ |
681 |
< |
public void testShutDownNow() { |
681 |
> |
public void testShutdownNow() { |
682 |
|
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
683 |
|
List l; |
684 |
|
try { |
696 |
|
|
697 |
|
// Exception Tests |
698 |
|
|
694 |
– |
|
699 |
|
/** |
700 |
|
* Constructor throws if corePoolSize argument is less than zero |
701 |
|
*/ |
756 |
|
} catch (NullPointerException success) {} |
757 |
|
} |
758 |
|
|
755 |
– |
|
756 |
– |
|
759 |
|
/** |
760 |
|
* Constructor throws if corePoolSize argument is less than zero |
761 |
|
*/ |
827 |
|
} catch (NullPointerException success) {} |
828 |
|
} |
829 |
|
|
828 |
– |
|
830 |
|
/** |
831 |
|
* Constructor throws if corePoolSize argument is less than zero |
832 |
|
*/ |
898 |
|
} catch (NullPointerException success) {} |
899 |
|
} |
900 |
|
|
900 |
– |
|
901 |
|
/** |
902 |
|
* Constructor throws if corePoolSize argument is less than zero |
903 |
|
*/ |
983 |
|
} catch (NullPointerException success) {} |
984 |
|
} |
985 |
|
|
986 |
– |
|
986 |
|
/** |
987 |
|
* execute throws RejectedExecutionException if saturated. |
988 |
|
*/ |
1132 |
|
} |
1133 |
|
} |
1134 |
|
|
1136 |
– |
|
1135 |
|
/** |
1136 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1137 |
|
*/ |
1149 |
|
} |
1150 |
|
} |
1151 |
|
|
1154 |
– |
|
1152 |
|
/** |
1153 |
|
* execute(null) throws NPE |
1154 |
|
*/ |
1213 |
|
joinPool(p); |
1214 |
|
} |
1215 |
|
|
1219 |
– |
|
1216 |
|
/** |
1217 |
|
* setKeepAliveTime throws IllegalArgumentException |
1218 |
|
* when given a negative value |
1237 |
|
public void testTerminated() { |
1238 |
|
CustomTPE p = new CustomTPE(); |
1239 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1240 |
< |
assertTrue(p.terminatedCalled); |
1240 |
> |
assertTrue(p.terminatedCalled()); |
1241 |
|
joinPool(p); |
1242 |
|
} |
1243 |
|
|
1247 |
|
public void testBeforeAfter() throws InterruptedException { |
1248 |
|
CustomTPE p = new CustomTPE(); |
1249 |
|
try { |
1250 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1251 |
< |
p.execute(r); |
1252 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1253 |
< |
assertTrue(r.done); |
1254 |
< |
assertTrue(p.beforeCalled); |
1255 |
< |
assertTrue(p.afterCalled); |
1250 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1251 |
> |
final CheckedRunnable task = new CheckedRunnable() { |
1252 |
> |
public void realRun() { |
1253 |
> |
done.countDown(); |
1254 |
> |
}}; |
1255 |
> |
p.execute(task); |
1256 |
> |
await(p.afterCalled); |
1257 |
> |
assertEquals(0, done.getCount()); |
1258 |
> |
assertTrue(p.afterCalled()); |
1259 |
> |
assertTrue(p.beforeCalled()); |
1260 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1261 |
|
} finally { |
1262 |
|
joinPool(p); |
1305 |
|
} |
1306 |
|
} |
1307 |
|
|
1308 |
– |
|
1308 |
|
/** |
1309 |
|
* invokeAny(null) throws NPE |
1310 |
|
*/ |
1466 |
|
} |
1467 |
|
} |
1468 |
|
|
1470 |
– |
|
1471 |
– |
|
1469 |
|
/** |
1470 |
|
* timed invokeAny(null) throws NPE |
1471 |
|
*/ |
1673 |
|
l.add(new StringTask()); |
1674 |
|
List<Future<String>> futures = |
1675 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1676 |
< |
assertEquals(3, futures.size()); |
1677 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1678 |
< |
Future<String> f1 = it.next(); |
1679 |
< |
Future<String> f2 = it.next(); |
1680 |
< |
Future<String> f3 = it.next(); |
1684 |
< |
assertTrue(f1.isDone()); |
1685 |
< |
assertTrue(f2.isDone()); |
1686 |
< |
assertTrue(f3.isDone()); |
1687 |
< |
assertFalse(f1.isCancelled()); |
1688 |
< |
assertTrue(f2.isCancelled()); |
1676 |
> |
assertEquals(l.size(), futures.size()); |
1677 |
> |
for (Future future : futures) |
1678 |
> |
assertTrue(future.isDone()); |
1679 |
> |
assertFalse(futures.get(0).isCancelled()); |
1680 |
> |
assertTrue(futures.get(1).isCancelled()); |
1681 |
|
} finally { |
1682 |
|
joinPool(e); |
1683 |
|
} |
1720 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1721 |
|
*/ |
1722 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1723 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1724 |
|
final ThreadPoolExecutor p = |
1725 |
|
new CustomTPE(2, 10, |
1726 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1726 |
> |
coreThreadTimeOut, MILLISECONDS, |
1727 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1728 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1729 |
|
try { |
1733 |
|
threadStarted.countDown(); |
1734 |
|
assertEquals(1, p.getPoolSize()); |
1735 |
|
}}); |
1736 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1737 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1738 |
< |
if (p.getPoolSize() == 0) |
1739 |
< |
break; |
1740 |
< |
Thread.sleep(10); |
1741 |
< |
} |
1736 |
> |
await(threadStarted); |
1737 |
> |
delay(coreThreadTimeOut); |
1738 |
> |
long startTime = System.nanoTime(); |
1739 |
> |
while (p.getPoolSize() > 0 |
1740 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1741 |
> |
Thread.yield(); |
1742 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1743 |
|
assertEquals(0, p.getPoolSize()); |
1744 |
|
} finally { |
1745 |
|
joinPool(p); |
1750 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1751 |
|
*/ |
1752 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1753 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1754 |
|
final ThreadPoolExecutor p = |
1755 |
|
new CustomTPE(2, 10, |
1756 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1756 |
> |
coreThreadTimeOut, MILLISECONDS, |
1757 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1758 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1759 |
|
try { |
1763 |
|
threadStarted.countDown(); |
1764 |
|
assertTrue(p.getPoolSize() >= 1); |
1765 |
|
}}); |
1766 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1766 |
> |
delay(2 * coreThreadTimeOut); |
1767 |
|
assertTrue(p.getPoolSize() >= 1); |
1768 |
|
} finally { |
1769 |
|
joinPool(p); |