1 /*
   2  * Copyright (c) 2007, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6458662
  27  * @summary poolSize might shrink below corePoolSize after timeout
  28  * @library /test/lib
  29  * @author Martin Buchholz
  30  */
  31 
  32 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  33 
  34 import java.util.concurrent.CyclicBarrier;
  35 import java.util.concurrent.SynchronousQueue;
  36 import java.util.concurrent.ThreadPoolExecutor;
  37 import jdk.test.lib.Utils;
  38 
  39 public class TimeOutShrink {
  40     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
  41     static final long KEEPALIVE_MS = 12L;
  42 
  43     static void checkPoolSizes(ThreadPoolExecutor pool,
  44                                int size, int core, int max) {
  45         equal(pool.getPoolSize(), size);
  46         equal(pool.getCorePoolSize(), core);
  47         equal(pool.getMaximumPoolSize(), max);
  48     }
  49 
  50     private static void realMain(String[] args) throws Throwable {
  51         final int n = 4;
  52         final CyclicBarrier barrier = new CyclicBarrier(2*n+1);
  53         final ThreadPoolExecutor pool
  54             = new ThreadPoolExecutor(n, 2*n,
  55                                      KEEPALIVE_MS, MILLISECONDS,
  56                                      new SynchronousQueue<Runnable>());
  57         final Runnable r = new Runnable() { public void run() {
  58             try {
  59                 barrier.await();
  60                 barrier.await();
  61             } catch (Throwable t) { unexpected(t); }}};
  62 
  63         for (int i = 0; i < 2*n; i++)
  64             pool.execute(r);
  65         barrier.await();
  66         checkPoolSizes(pool, 2*n, n, 2*n);
  67         barrier.await();
  68         long nap = KEEPALIVE_MS + (KEEPALIVE_MS >> 2);
  69         for (long sleepyTime = 0L; pool.getPoolSize() > n; ) {
  70             check((sleepyTime += nap) <= LONG_DELAY_MS);
  71             Thread.sleep(nap);
  72         }
  73         checkPoolSizes(pool, n, n, 2*n);
  74         Thread.sleep(nap);
  75         checkPoolSizes(pool, n, n, 2*n);
  76         pool.shutdown();
  77         check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
  78     }
  79 
  80     //--------------------- Infrastructure ---------------------------
  81     static volatile int passed = 0, failed = 0;
  82     static void pass() {passed++;}
  83     static void fail() {failed++; Thread.dumpStack();}
  84     static void fail(String msg) {System.out.println(msg); fail();}
  85     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
  86     static void check(boolean cond) {if (cond) pass(); else fail();}
  87     static void equal(Object x, Object y) {
  88         if (x == null ? y == null : x.equals(y)) pass();
  89         else fail(x + " not equal to " + y);}
  90     public static void main(String[] args) throws Throwable {
  91         try {realMain(args);} catch (Throwable t) {unexpected(t);}
  92         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  93         if (failed > 0) throw new AssertionError("Some tests failed");}
  94 }