jdk/test/java/util/concurrent/ScheduledThreadPoolExecutor/ZeroCoreThreads.java
changeset 32991 b27c76b82713
child 34347 4a17f9e90a0f
equal deleted inserted replaced
32990:299a81977f48 32991:b27c76b82713
       
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.
       
     7  *
       
     8  * This code is distributed in the hope that it will be useful, but WITHOUT
       
     9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    11  * version 2 for more details (a copy is included in the LICENSE file that
       
    12  * accompanied this code).
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License version
       
    15  * 2 along with this work; if not, write to the Free Software Foundation,
       
    16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    17  *
       
    18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    19  * or visit www.oracle.com if you need additional information or have any
       
    20  * questions.
       
    21  */
       
    22 
       
    23 /*
       
    24  * This file is available under and governed by the GNU General Public
       
    25  * License version 2 only, as published by the Free Software Foundation.
       
    26  * However, the following notice accompanied the original version of this
       
    27  * file:
       
    28  *
       
    29  * Written by Martin Buchholz with assistance from members of JCP
       
    30  * JSR-166 Expert Group and released to the public domain, as
       
    31  * explained at http://creativecommons.org/publicdomain/zero/1.0/
       
    32  */
       
    33 
       
    34 /*
       
    35  * @test
       
    36  * @bug 8022642 8065320 8129861
       
    37  * @summary Ensure relative sanity when zero core threads
       
    38  */
       
    39 
       
    40 import java.util.concurrent.*;
       
    41 import static java.util.concurrent.TimeUnit.*;
       
    42 import java.util.concurrent.locks.*;
       
    43 import java.lang.reflect.*;
       
    44 
       
    45 public class ZeroCoreThreads {
       
    46     static boolean hasWaiters(ReentrantLock lock, Condition condition) {
       
    47         lock.lock();
       
    48         try {
       
    49             return lock.hasWaiters(condition);
       
    50         } finally {
       
    51             lock.unlock();
       
    52         }
       
    53     }
       
    54 
       
    55     static <T> T getField(Object x, String fieldName) {
       
    56         try {
       
    57             Field field = x.getClass().getDeclaredField(fieldName);
       
    58             field.setAccessible(true);
       
    59             return (T) field.get(x);
       
    60         } catch (ReflectiveOperationException ex) {
       
    61             throw new AssertionError(ex);
       
    62         }
       
    63     }
       
    64 
       
    65     void test(String[] args) throws Throwable {
       
    66         ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(0);
       
    67         try {
       
    68             test(p);
       
    69         } finally {
       
    70             p.shutdownNow();
       
    71             check(p.awaitTermination(10L, SECONDS));
       
    72         }
       
    73     }
       
    74 
       
    75     void test(ScheduledThreadPoolExecutor p) throws Throwable {
       
    76         Runnable dummy = new Runnable() { public void run() {
       
    77             throw new AssertionError("shouldn't get here"); }};
       
    78         BlockingQueue q = p.getQueue();
       
    79         ReentrantLock lock = getField(q, "lock");
       
    80         Condition available = getField(q, "available");
       
    81 
       
    82         equal(0, p.getPoolSize());
       
    83         equal(0, p.getLargestPoolSize());
       
    84         equal(0L, p.getTaskCount());
       
    85         equal(0L, p.getCompletedTaskCount());
       
    86         p.schedule(dummy, 1L, HOURS);
       
    87         // Ensure one pool thread actually waits in timed queue poll
       
    88         long t0 = System.nanoTime();
       
    89         while (!hasWaiters(lock, available)) {
       
    90             if (System.nanoTime() - t0 > SECONDS.toNanos(10L))
       
    91                 throw new AssertionError
       
    92                     ("timed out waiting for a waiter to show up");
       
    93             Thread.yield();
       
    94         }
       
    95         equal(1, p.getPoolSize());
       
    96         equal(1, p.getLargestPoolSize());
       
    97         equal(1L, p.getTaskCount());
       
    98         equal(0L, p.getCompletedTaskCount());
       
    99     }
       
   100 
       
   101     //--------------------- Infrastructure ---------------------------
       
   102     volatile int passed = 0, failed = 0;
       
   103     void pass() {passed++;}
       
   104     void fail() {failed++; Thread.dumpStack();}
       
   105     void fail(String msg) {System.err.println(msg); fail();}
       
   106     void unexpected(Throwable t) {failed++; t.printStackTrace();}
       
   107     void check(boolean cond) {if (cond) pass(); else fail();}
       
   108     void equal(Object x, Object y) {
       
   109         if (x == null ? y == null : x.equals(y)) pass();
       
   110         else fail(x + " not equal to " + y);}
       
   111     public static void main(String[] args) throws Throwable {
       
   112         new ZeroCoreThreads().instanceMain(args);}
       
   113     void instanceMain(String[] args) throws Throwable {
       
   114         try {test(args);} catch (Throwable t) {unexpected(t);}
       
   115         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
       
   116         if (failed > 0) throw new AssertionError("Some tests failed");}
       
   117 }