132 |
|
*/ |
133 |
|
public void testPrestartCoreThread() { |
134 |
|
final ThreadPoolExecutor p = |
135 |
< |
new ThreadPoolExecutor(2, 2, |
135 |
> |
new ThreadPoolExecutor(2, 6, |
136 |
|
LONG_DELAY_MS, MILLISECONDS, |
137 |
|
new ArrayBlockingQueue<Runnable>(10)); |
138 |
< |
assertEquals(0, p.getPoolSize()); |
139 |
< |
assertTrue(p.prestartCoreThread()); |
140 |
< |
assertEquals(1, p.getPoolSize()); |
141 |
< |
assertTrue(p.prestartCoreThread()); |
142 |
< |
assertEquals(2, p.getPoolSize()); |
143 |
< |
assertFalse(p.prestartCoreThread()); |
144 |
< |
assertEquals(2, p.getPoolSize()); |
145 |
< |
joinPool(p); |
138 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
139 |
> |
assertEquals(0, p.getPoolSize()); |
140 |
> |
assertTrue(p.prestartCoreThread()); |
141 |
> |
assertEquals(1, p.getPoolSize()); |
142 |
> |
assertTrue(p.prestartCoreThread()); |
143 |
> |
assertEquals(2, p.getPoolSize()); |
144 |
> |
assertFalse(p.prestartCoreThread()); |
145 |
> |
assertEquals(2, p.getPoolSize()); |
146 |
> |
p.setCorePoolSize(4); |
147 |
> |
assertTrue(p.prestartCoreThread()); |
148 |
> |
assertEquals(3, p.getPoolSize()); |
149 |
> |
assertTrue(p.prestartCoreThread()); |
150 |
> |
assertEquals(4, p.getPoolSize()); |
151 |
> |
assertFalse(p.prestartCoreThread()); |
152 |
> |
assertEquals(4, p.getPoolSize()); |
153 |
> |
} |
154 |
|
} |
155 |
|
|
156 |
|
/** |
158 |
|
*/ |
159 |
|
public void testPrestartAllCoreThreads() { |
160 |
|
final ThreadPoolExecutor p = |
161 |
< |
new ThreadPoolExecutor(2, 2, |
161 |
> |
new ThreadPoolExecutor(2, 6, |
162 |
|
LONG_DELAY_MS, MILLISECONDS, |
163 |
|
new ArrayBlockingQueue<Runnable>(10)); |
164 |
< |
assertEquals(0, p.getPoolSize()); |
165 |
< |
p.prestartAllCoreThreads(); |
166 |
< |
assertEquals(2, p.getPoolSize()); |
167 |
< |
p.prestartAllCoreThreads(); |
168 |
< |
assertEquals(2, p.getPoolSize()); |
169 |
< |
joinPool(p); |
164 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
165 |
> |
assertEquals(0, p.getPoolSize()); |
166 |
> |
p.prestartAllCoreThreads(); |
167 |
> |
assertEquals(2, p.getPoolSize()); |
168 |
> |
p.prestartAllCoreThreads(); |
169 |
> |
assertEquals(2, p.getPoolSize()); |
170 |
> |
p.setCorePoolSize(4); |
171 |
> |
p.prestartAllCoreThreads(); |
172 |
> |
assertEquals(4, p.getPoolSize()); |
173 |
> |
p.prestartAllCoreThreads(); |
174 |
> |
assertEquals(4, p.getPoolSize()); |
175 |
> |
} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
184 |
|
new ThreadPoolExecutor(2, 2, |
185 |
|
LONG_DELAY_MS, MILLISECONDS, |
186 |
|
new ArrayBlockingQueue<Runnable>(10)); |
187 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
188 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
189 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
190 |
< |
try { |
187 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
188 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
189 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
190 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
191 |
|
assertEquals(0, p.getCompletedTaskCount()); |
192 |
|
p.execute(new CheckedRunnable() { |
193 |
|
public void realRun() throws InterruptedException { |
206 |
|
fail("timed out"); |
207 |
|
Thread.yield(); |
208 |
|
} |
195 |
– |
} finally { |
196 |
– |
joinPool(p); |
209 |
|
} |
210 |
|
} |
211 |
|
|
217 |
|
new ThreadPoolExecutor(1, 1, |
218 |
|
LONG_DELAY_MS, MILLISECONDS, |
219 |
|
new ArrayBlockingQueue<Runnable>(10)); |
220 |
< |
assertEquals(1, p.getCorePoolSize()); |
221 |
< |
joinPool(p); |
220 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
221 |
> |
assertEquals(1, p.getCorePoolSize()); |
222 |
> |
} |
223 |
|
} |
224 |
|
|
225 |
|
/** |
650 |
|
LONG_DELAY_MS, MILLISECONDS, |
651 |
|
new ArrayBlockingQueue<Runnable>(10)); |
652 |
|
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
653 |
< |
CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { |
653 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
654 |
|
threadsStarted.countDown(); |
655 |
|
try { |
656 |
|
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
660 |
|
for (int i = 0; i < count; i++) |
661 |
|
p.execute(waiter); |
662 |
|
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
663 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
664 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
665 |
|
final List<Runnable> queuedTasks; |
666 |
|
try { |
667 |
|
queuedTasks = p.shutdownNow(); |
674 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
675 |
|
assertTrue(p.isTerminated()); |
676 |
|
assertEquals(poolSize, ran.get()); |
677 |
+ |
assertEquals(poolSize, p.getCompletedTaskCount()); |
678 |
|
} |
679 |
|
|
680 |
|
// Exception Tests |
1907 |
|
l.add(new StringTask()); |
1908 |
|
l.add(new StringTask()); |
1909 |
|
List<Future<String>> futures = |
1910 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1910 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1911 |
|
assertEquals(2, futures.size()); |
1912 |
|
for (Future<String> future : futures) |
1913 |
|
assertSame(TEST_STRING, future.get()); |
2075 |
|
} |
2076 |
|
} |
2077 |
|
|
2078 |
+ |
/** |
2079 |
+ |
* get(cancelled task) throws CancellationException |
2080 |
+ |
*/ |
2081 |
+ |
public void testGet_cancelled() throws Exception { |
2082 |
+ |
final ExecutorService e = |
2083 |
+ |
new ThreadPoolExecutor(1, 1, |
2084 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
2085 |
+ |
new LinkedBlockingQueue<Runnable>()); |
2086 |
+ |
try { |
2087 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2088 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2089 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
2090 |
+ |
for (int i = 0; i < 2; i++) { |
2091 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
2092 |
+ |
throws Throwable { |
2093 |
+ |
blockerStarted.countDown(); |
2094 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2095 |
+ |
}}; |
2096 |
+ |
futures.add(e.submit(r)); |
2097 |
+ |
} |
2098 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2099 |
+ |
for (Future<?> future : futures) future.cancel(false); |
2100 |
+ |
for (Future<?> future : futures) { |
2101 |
+ |
try { |
2102 |
+ |
future.get(); |
2103 |
+ |
shouldThrow(); |
2104 |
+ |
} catch (CancellationException success) {} |
2105 |
+ |
try { |
2106 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2107 |
+ |
shouldThrow(); |
2108 |
+ |
} catch (CancellationException success) {} |
2109 |
+ |
assertTrue(future.isCancelled()); |
2110 |
+ |
assertTrue(future.isDone()); |
2111 |
+ |
} |
2112 |
+ |
done.countDown(); |
2113 |
+ |
} finally { |
2114 |
+ |
joinPool(e); |
2115 |
+ |
} |
2116 |
+ |
} |
2117 |
+ |
|
2118 |
|
} |