blob: cdf1860c7726908c2db5957a611b73a1aa5d59ec [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6458662
27 * @summary poolSize might shrink below corePoolSize after timeout
28 * @author Martin Buchholz
29 */
30
31import java.util.*;
32import java.util.concurrent.*;
33
34public class TimeOutShrink {
35 static void checkPoolSizes(ThreadPoolExecutor pool,
36 int size, int core, int max) {
37 equal(pool.getPoolSize(), size);
38 equal(pool.getCorePoolSize(), core);
39 equal(pool.getMaximumPoolSize(), max);
40 }
41
42 private static void realMain(String[] args) throws Throwable {
43 final int n = 4;
44 final CyclicBarrier barrier = new CyclicBarrier(2*n+1);
45 final ThreadPoolExecutor pool
46 = new ThreadPoolExecutor(n, 2*n, 1L, TimeUnit.SECONDS,
47 new SynchronousQueue<Runnable>());
48 final Runnable r = new Runnable() { public void run() {
49 try {
50 barrier.await();
51 barrier.await();
52 } catch (Throwable t) { unexpected(t); }}};
53
54 for (int i = 0; i < 2*n; i++)
55 pool.execute(r);
56 barrier.await();
57 checkPoolSizes(pool, 2*n, n, 2*n);
58 barrier.await();
59 while (pool.getPoolSize() > n)
60 Thread.sleep(100);
61 Thread.sleep(100);
62 checkPoolSizes(pool, n, n, 2*n);
63 pool.shutdown();
64 pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
65 }
66
67 //--------------------- Infrastructure ---------------------------
68 static volatile int passed = 0, failed = 0;
69 static void pass() {passed++;}
70 static void fail() {failed++; Thread.dumpStack();}
71 static void fail(String msg) {System.out.println(msg); fail();}
72 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
73 static void check(boolean cond) {if (cond) pass(); else fail();}
74 static void equal(Object x, Object y) {
75 if (x == null ? y == null : x.equals(y)) pass();
76 else fail(x + " not equal to " + y);}
77 public static void main(String[] args) throws Throwable {
78 try {realMain(args);} catch (Throwable t) {unexpected(t);}
79 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
80 if (failed > 0) throw new AssertionError("Some tests failed");}
81}