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 6576792
  27  * @summary non-idle worker threads should not be interrupted
  28  * @library /test/lib
  29  */
  30 
  31 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  32 
  33 import java.util.concurrent.CountDownLatch;
  34 import java.util.concurrent.SynchronousQueue;
  35 import java.util.concurrent.ThreadPoolExecutor;
  36 import java.util.concurrent.TimeUnit;
  37 import jdk.test.lib.Utils;
  38 
  39 public class SelfInterrupt {
  40     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
  41 
  42     void test(String[] args) throws Throwable {
  43         final int n = 100;
  44         final ThreadPoolExecutor pool =
  45             new ThreadPoolExecutor(n, n, 1L, TimeUnit.NANOSECONDS,
  46                                    new SynchronousQueue<Runnable>());
  47         final CountDownLatch startingGate = new CountDownLatch(n);
  48         final CountDownLatch finishLine = new CountDownLatch(n);
  49         equal(pool.getCorePoolSize(), n);
  50         equal(pool.getPoolSize(), 0);
  51         for (int i = 0; i < n; i++)
  52             pool.execute(new Runnable() { public void run() {
  53                 try {
  54                     startingGate.countDown();
  55                     startingGate.await();
  56                     equal(pool.getPoolSize(), n);
  57                     pool.setCorePoolSize(n);
  58                     pool.setCorePoolSize(1);
  59                     check(! Thread.interrupted());
  60                     equal(pool.getPoolSize(), n);
  61                     finishLine.countDown();
  62                     finishLine.await();
  63                     check(! Thread.interrupted());
  64                 } catch (Throwable t) { unexpected(t); }}});
  65         finishLine.await();
  66         pool.shutdown();
  67         check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
  68     }
  69 
  70     //--------------------- Infrastructure ---------------------------
  71     volatile int passed = 0, failed = 0;
  72     void pass() {passed++;}
  73     void fail() {failed++; Thread.dumpStack();}
  74     void fail(String msg) {System.err.println(msg); fail();}
  75     void unexpected(Throwable t) {failed++; t.printStackTrace();}
  76     void check(boolean cond) {if (cond) pass(); else fail();}
  77     void equal(Object x, Object y) {
  78         if (x == null ? y == null : x.equals(y)) pass();
  79         else fail(x + " not equal to " + y);}
  80     public static void main(String[] args) throws Throwable {
  81         new SelfInterrupt().instanceMain(args);}
  82     void instanceMain(String[] args) throws Throwable {
  83         try {test(args);} catch (Throwable t) {unexpected(t);}
  84         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
  85         if (failed > 0) throw new AssertionError("Some tests failed");}
  86 }