ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.65 by jsr166, Sat Oct 3 00:37:32 2015 UTC vs.
Revision 1.76 by jsr166, Sun Oct 4 02:04:56 2015 UTC

# Line 132 | Line 132 | public class ThreadPoolExecutorTest exte
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      /**
# Line 150 | Line 158 | public class ThreadPoolExecutorTest exte
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      /**
# Line 170 | Line 184 | public class ThreadPoolExecutorTest exte
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 {
# Line 192 | Line 206 | public class ThreadPoolExecutorTest exte
206                      fail("timed out");
207                  Thread.yield();
208              }
195        } finally {
196            joinPool(p);
209          }
210      }
211  
# Line 205 | Line 217 | public class ThreadPoolExecutorTest exte
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      /**
# Line 217 | Line 230 | public class ThreadPoolExecutorTest exte
230              new ThreadPoolExecutor(2, 2,
231                                     1000, MILLISECONDS,
232                                     new ArrayBlockingQueue<Runnable>(10));
233 <        assertEquals(1, p.getKeepAliveTime(SECONDS));
234 <        joinPool(p);
233 >        try (PoolCleaner cleaner = cleaner(p)) {
234 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
235 >        }
236      }
237  
238      /**
239       * getThreadFactory returns factory in constructor if not set
240       */
241      public void testGetThreadFactory() {
242 <        ThreadFactory tf = new SimpleThreadFactory();
242 >        ThreadFactory threadFactory = new SimpleThreadFactory();
243          final ThreadPoolExecutor p =
244              new ThreadPoolExecutor(1, 2,
245                                     LONG_DELAY_MS, MILLISECONDS,
246                                     new ArrayBlockingQueue<Runnable>(10),
247 <                                   tf,
247 >                                   threadFactory,
248                                     new NoOpREHandler());
249 <        assertSame(tf, p.getThreadFactory());
250 <        joinPool(p);
249 >        try (PoolCleaner cleaner = cleaner(p)) {
250 >            assertSame(threadFactory, p.getThreadFactory());
251 >        }
252      }
253  
254      /**
# Line 244 | Line 259 | public class ThreadPoolExecutorTest exte
259              new ThreadPoolExecutor(1, 2,
260                                     LONG_DELAY_MS, MILLISECONDS,
261                                     new ArrayBlockingQueue<Runnable>(10));
262 <        ThreadFactory tf = new SimpleThreadFactory();
263 <        p.setThreadFactory(tf);
264 <        assertSame(tf, p.getThreadFactory());
265 <        joinPool(p);
262 >        try (PoolCleaner cleaner = cleaner(p)) {
263 >            ThreadFactory threadFactory = new SimpleThreadFactory();
264 >            p.setThreadFactory(threadFactory);
265 >            assertSame(threadFactory, p.getThreadFactory());
266 >        }
267      }
268  
269      /**
# Line 258 | Line 274 | public class ThreadPoolExecutorTest exte
274              new ThreadPoolExecutor(1, 2,
275                                     LONG_DELAY_MS, MILLISECONDS,
276                                     new ArrayBlockingQueue<Runnable>(10));
277 <        try {
278 <            p.setThreadFactory(null);
279 <            shouldThrow();
280 <        } catch (NullPointerException success) {
281 <        } finally {
266 <            joinPool(p);
277 >        try (PoolCleaner cleaner = cleaner(p)) {
278 >            try {
279 >                p.setThreadFactory(null);
280 >                shouldThrow();
281 >            } catch (NullPointerException success) {}
282          }
283      }
284  
# Line 271 | Line 286 | public class ThreadPoolExecutorTest exte
286       * getRejectedExecutionHandler returns handler in constructor if not set
287       */
288      public void testGetRejectedExecutionHandler() {
289 <        final RejectedExecutionHandler h = new NoOpREHandler();
289 >        final RejectedExecutionHandler handler = new NoOpREHandler();
290          final ThreadPoolExecutor p =
291              new ThreadPoolExecutor(1, 2,
292                                     LONG_DELAY_MS, MILLISECONDS,
293                                     new ArrayBlockingQueue<Runnable>(10),
294 <                                   h);
295 <        assertSame(h, p.getRejectedExecutionHandler());
296 <        joinPool(p);
294 >                                   handler);
295 >        try (PoolCleaner cleaner = cleaner(p)) {
296 >            assertSame(handler, p.getRejectedExecutionHandler());
297 >        }
298      }
299  
300      /**
# Line 290 | Line 306 | public class ThreadPoolExecutorTest exte
306              new ThreadPoolExecutor(1, 2,
307                                     LONG_DELAY_MS, MILLISECONDS,
308                                     new ArrayBlockingQueue<Runnable>(10));
309 <        RejectedExecutionHandler h = new NoOpREHandler();
310 <        p.setRejectedExecutionHandler(h);
311 <        assertSame(h, p.getRejectedExecutionHandler());
312 <        joinPool(p);
309 >        try (PoolCleaner cleaner = cleaner(p)) {
310 >            RejectedExecutionHandler handler = new NoOpREHandler();
311 >            p.setRejectedExecutionHandler(handler);
312 >            assertSame(handler, p.getRejectedExecutionHandler());
313 >        }
314      }
315  
316      /**
# Line 304 | Line 321 | public class ThreadPoolExecutorTest exte
321              new ThreadPoolExecutor(1, 2,
322                                     LONG_DELAY_MS, MILLISECONDS,
323                                     new ArrayBlockingQueue<Runnable>(10));
324 <        try {
325 <            p.setRejectedExecutionHandler(null);
326 <            shouldThrow();
327 <        } catch (NullPointerException success) {
328 <        } finally {
312 <            joinPool(p);
324 >        try (PoolCleaner cleaner = cleaner(p)) {
325 >            try {
326 >                p.setRejectedExecutionHandler(null);
327 >                shouldThrow();
328 >            } catch (NullPointerException success) {}
329          }
330      }
331  
# Line 323 | Line 339 | public class ThreadPoolExecutorTest exte
339              new ThreadPoolExecutor(THREADS, THREADS,
340                                     LONG_DELAY_MS, MILLISECONDS,
341                                     new ArrayBlockingQueue<Runnable>(10));
342 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
343 <        final CountDownLatch done = new CountDownLatch(1);
344 <        try {
342 >        try (PoolCleaner cleaner = cleaner(p)) {
343 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
344 >            final CountDownLatch done = new CountDownLatch(1);
345              assertEquals(0, p.getLargestPoolSize());
346              for (int i = 0; i < THREADS; i++)
347                  p.execute(new CheckedRunnable() {
# Line 334 | Line 350 | public class ThreadPoolExecutorTest exte
350                          done.await();
351                          assertEquals(THREADS, p.getLargestPoolSize());
352                      }});
353 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
338 <            assertEquals(THREADS, p.getLargestPoolSize());
339 <        } finally {
340 <            done.countDown();
341 <            joinPool(p);
353 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
354              assertEquals(THREADS, p.getLargestPoolSize());
355 +            done.countDown();   // release pool
356          }
357 +        assertEquals(THREADS, p.getLargestPoolSize());
358      }
359  
360      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines