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 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
– |
import java.util.concurrent.locks.*; |
10 |
|
|
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.List; |
13 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
14 |
> |
import java.util.concurrent.BlockingQueue; |
15 |
> |
import java.util.concurrent.Callable; |
16 |
> |
import java.util.concurrent.CountDownLatch; |
17 |
> |
import java.util.concurrent.ExecutionException; |
18 |
> |
import java.util.concurrent.Executors; |
19 |
> |
import java.util.concurrent.ExecutorService; |
20 |
> |
import java.util.concurrent.Future; |
21 |
> |
import java.util.concurrent.FutureTask; |
22 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
23 |
> |
import java.util.concurrent.RejectedExecutionException; |
24 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
25 |
> |
import java.util.concurrent.RunnableFuture; |
26 |
> |
import java.util.concurrent.SynchronousQueue; |
27 |
> |
import java.util.concurrent.ThreadFactory; |
28 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
29 |
> |
import java.util.concurrent.TimeoutException; |
30 |
> |
import java.util.concurrent.TimeUnit; |
31 |
> |
import java.util.concurrent.locks.Condition; |
32 |
> |
import java.util.concurrent.locks.ReentrantLock; |
33 |
> |
|
34 |
> |
import junit.framework.Test; |
35 |
> |
import junit.framework.TestSuite; |
36 |
|
|
37 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
38 |
|
public static void main(String[] args) { |
136 |
|
} |
137 |
|
} |
138 |
|
|
118 |
– |
|
139 |
|
static class CustomTPE extends ThreadPoolExecutor { |
140 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
141 |
|
return new CustomTask<V>(c); |
182 |
|
workQueue, threadFactory, handler); |
183 |
|
} |
184 |
|
|
185 |
< |
volatile boolean beforeCalled = false; |
186 |
< |
volatile boolean afterCalled = false; |
187 |
< |
volatile boolean terminatedCalled = false; |
185 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
186 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
187 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
188 |
> |
|
189 |
|
public CustomTPE() { |
190 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
191 |
|
} |
192 |
|
protected void beforeExecute(Thread t, Runnable r) { |
193 |
< |
beforeCalled = true; |
193 |
> |
beforeCalled.countDown(); |
194 |
|
} |
195 |
|
protected void afterExecute(Runnable r, Throwable t) { |
196 |
< |
afterCalled = true; |
196 |
> |
afterCalled.countDown(); |
197 |
|
} |
198 |
|
protected void terminated() { |
199 |
< |
terminatedCalled = true; |
199 |
> |
terminatedCalled.countDown(); |
200 |
|
} |
201 |
|
|
202 |
+ |
public boolean beforeCalled() { |
203 |
+ |
return beforeCalled.getCount() == 0; |
204 |
+ |
} |
205 |
+ |
public boolean afterCalled() { |
206 |
+ |
return afterCalled.getCount() == 0; |
207 |
+ |
} |
208 |
+ |
public boolean terminatedCalled() { |
209 |
+ |
return terminatedCalled.getCount() == 0; |
210 |
+ |
} |
211 |
|
} |
212 |
|
|
213 |
|
static class FailingThreadFactory implements ThreadFactory { |
218 |
|
} |
219 |
|
} |
220 |
|
|
191 |
– |
|
221 |
|
/** |
222 |
|
* execute successfully executes a runnable |
223 |
|
*/ |
315 |
|
threadProceed.await(); |
316 |
|
threadDone.countDown(); |
317 |
|
}}); |
318 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
318 |
> |
await(threadStarted); |
319 |
|
assertEquals(0, p.getCompletedTaskCount()); |
320 |
|
threadProceed.countDown(); |
321 |
|
threadDone.await(); |
322 |
< |
Thread.sleep(SHORT_DELAY_MS); |
323 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
322 |
> |
long startTime = System.nanoTime(); |
323 |
> |
while (p.getCompletedTaskCount() != 1) { |
324 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
325 |
> |
fail("timed out"); |
326 |
> |
Thread.yield(); |
327 |
> |
} |
328 |
|
} finally { |
329 |
|
joinPool(p); |
330 |
|
} |
348 |
|
joinPool(p); |
349 |
|
} |
350 |
|
|
318 |
– |
|
351 |
|
/** |
352 |
|
* getThreadFactory returns factory in constructor if not set |
353 |
|
*/ |
369 |
|
joinPool(p); |
370 |
|
} |
371 |
|
|
340 |
– |
|
372 |
|
/** |
373 |
|
* setThreadFactory(null) throws NPE |
374 |
|
*/ |
405 |
|
joinPool(p); |
406 |
|
} |
407 |
|
|
377 |
– |
|
408 |
|
/** |
409 |
|
* setRejectedExecutionHandler(null) throws NPE |
410 |
|
*/ |
419 |
|
} |
420 |
|
} |
421 |
|
|
392 |
– |
|
422 |
|
/** |
423 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
424 |
|
* multiple threads active |
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
< |
* isShutDown is false before shutdown, true after |
516 |
> |
* isShutdown is false before shutdown, true after |
517 |
|
*/ |
518 |
|
public void testIsShutdown() { |
519 |
|
|
524 |
|
joinPool(p); |
525 |
|
} |
526 |
|
|
498 |
– |
|
527 |
|
/** |
528 |
|
* isTerminated is false before termination, true after |
529 |
|
*/ |
538 |
|
assertFalse(p.isTerminating()); |
539 |
|
p.execute(new CheckedRunnable() { |
540 |
|
public void realRun() throws InterruptedException { |
513 |
– |
threadStarted.countDown(); |
541 |
|
assertFalse(p.isTerminating()); |
542 |
+ |
threadStarted.countDown(); |
543 |
|
done.await(); |
544 |
|
}}); |
545 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
697 |
|
} |
698 |
|
|
699 |
|
/** |
700 |
< |
* shutDownNow returns a list containing tasks that were not run |
700 |
> |
* shutdownNow returns a list containing tasks that were not run |
701 |
|
*/ |
702 |
< |
public void testShutDownNow() { |
702 |
> |
public void testShutdownNow() { |
703 |
|
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
704 |
|
List l; |
705 |
|
try { |
717 |
|
|
718 |
|
// Exception Tests |
719 |
|
|
692 |
– |
|
720 |
|
/** |
721 |
|
* Constructor throws if corePoolSize argument is less than zero |
722 |
|
*/ |
777 |
|
} catch (NullPointerException success) {} |
778 |
|
} |
779 |
|
|
753 |
– |
|
754 |
– |
|
780 |
|
/** |
781 |
|
* Constructor throws if corePoolSize argument is less than zero |
782 |
|
*/ |
848 |
|
} catch (NullPointerException success) {} |
849 |
|
} |
850 |
|
|
826 |
– |
|
851 |
|
/** |
852 |
|
* Constructor throws if corePoolSize argument is less than zero |
853 |
|
*/ |
919 |
|
} catch (NullPointerException success) {} |
920 |
|
} |
921 |
|
|
898 |
– |
|
922 |
|
/** |
923 |
|
* Constructor throws if corePoolSize argument is less than zero |
924 |
|
*/ |
1004 |
|
} catch (NullPointerException success) {} |
1005 |
|
} |
1006 |
|
|
984 |
– |
|
1007 |
|
/** |
1008 |
|
* execute throws RejectedExecutionException if saturated. |
1009 |
|
*/ |
1153 |
|
} |
1154 |
|
} |
1155 |
|
|
1134 |
– |
|
1156 |
|
/** |
1157 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1158 |
|
*/ |
1170 |
|
} |
1171 |
|
} |
1172 |
|
|
1152 |
– |
|
1173 |
|
/** |
1174 |
|
* execute(null) throws NPE |
1175 |
|
*/ |
1234 |
|
joinPool(p); |
1235 |
|
} |
1236 |
|
|
1217 |
– |
|
1237 |
|
/** |
1238 |
|
* setKeepAliveTime throws IllegalArgumentException |
1239 |
|
* when given a negative value |
1258 |
|
public void testTerminated() { |
1259 |
|
CustomTPE p = new CustomTPE(); |
1260 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1261 |
< |
assertTrue(p.terminatedCalled); |
1261 |
> |
assertTrue(p.terminatedCalled()); |
1262 |
|
joinPool(p); |
1263 |
|
} |
1264 |
|
|
1268 |
|
public void testBeforeAfter() throws InterruptedException { |
1269 |
|
CustomTPE p = new CustomTPE(); |
1270 |
|
try { |
1271 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1272 |
< |
p.execute(r); |
1273 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1274 |
< |
assertTrue(r.done); |
1275 |
< |
assertTrue(p.beforeCalled); |
1276 |
< |
assertTrue(p.afterCalled); |
1271 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1272 |
> |
p.execute(new CheckedRunnable() { |
1273 |
> |
public void realRun() { |
1274 |
> |
done.countDown(); |
1275 |
> |
}}); |
1276 |
> |
await(p.afterCalled); |
1277 |
> |
assertEquals(0, done.getCount()); |
1278 |
> |
assertTrue(p.afterCalled()); |
1279 |
> |
assertTrue(p.beforeCalled()); |
1280 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1281 |
|
} finally { |
1282 |
|
joinPool(p); |
1325 |
|
} |
1326 |
|
} |
1327 |
|
|
1306 |
– |
|
1328 |
|
/** |
1329 |
|
* invokeAny(null) throws NPE |
1330 |
|
*/ |
1486 |
|
} |
1487 |
|
} |
1488 |
|
|
1468 |
– |
|
1469 |
– |
|
1489 |
|
/** |
1490 |
|
* timed invokeAny(null) throws NPE |
1491 |
|
*/ |
1693 |
|
l.add(new StringTask()); |
1694 |
|
List<Future<String>> futures = |
1695 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1696 |
< |
assertEquals(3, futures.size()); |
1697 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1698 |
< |
Future<String> f1 = it.next(); |
1699 |
< |
Future<String> f2 = it.next(); |
1700 |
< |
Future<String> f3 = it.next(); |
1682 |
< |
assertTrue(f1.isDone()); |
1683 |
< |
assertTrue(f2.isDone()); |
1684 |
< |
assertTrue(f3.isDone()); |
1685 |
< |
assertFalse(f1.isCancelled()); |
1686 |
< |
assertTrue(f2.isCancelled()); |
1696 |
> |
assertEquals(l.size(), futures.size()); |
1697 |
> |
for (Future future : futures) |
1698 |
> |
assertTrue(future.isDone()); |
1699 |
> |
assertFalse(futures.get(0).isCancelled()); |
1700 |
> |
assertTrue(futures.get(1).isCancelled()); |
1701 |
|
} finally { |
1702 |
|
joinPool(e); |
1703 |
|
} |
1740 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1741 |
|
*/ |
1742 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1743 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1744 |
|
final ThreadPoolExecutor p = |
1745 |
|
new CustomTPE(2, 10, |
1746 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1746 |
> |
coreThreadTimeOut, MILLISECONDS, |
1747 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1748 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1749 |
|
try { |
1753 |
|
threadStarted.countDown(); |
1754 |
|
assertEquals(1, p.getPoolSize()); |
1755 |
|
}}); |
1756 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1757 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1758 |
< |
if (p.getPoolSize() == 0) |
1759 |
< |
break; |
1760 |
< |
Thread.sleep(10); |
1761 |
< |
} |
1756 |
> |
await(threadStarted); |
1757 |
> |
delay(coreThreadTimeOut); |
1758 |
> |
long startTime = System.nanoTime(); |
1759 |
> |
while (p.getPoolSize() > 0 |
1760 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1761 |
> |
Thread.yield(); |
1762 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1763 |
|
assertEquals(0, p.getPoolSize()); |
1764 |
|
} finally { |
1765 |
|
joinPool(p); |
1770 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1771 |
|
*/ |
1772 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1773 |
+ |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1774 |
|
final ThreadPoolExecutor p = |
1775 |
|
new CustomTPE(2, 10, |
1776 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1776 |
> |
coreThreadTimeOut, MILLISECONDS, |
1777 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1778 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1779 |
|
try { |
1783 |
|
threadStarted.countDown(); |
1784 |
|
assertTrue(p.getPoolSize() >= 1); |
1785 |
|
}}); |
1786 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1786 |
> |
delay(2 * coreThreadTimeOut); |
1787 |
|
assertTrue(p.getPoolSize() >= 1); |
1788 |
|
} finally { |
1789 |
|
joinPool(p); |