jdk/test/java/lang/management/ThreadMXBean/ThreadDaemonTest.java
changeset 29101 55f7a91aaa32
child 30376 2ccf2cf7ea48
equal deleted inserted replaced
29100:56267f38f743 29101:55f7a91aaa32
       
     1 /*
       
     2  * Copyright (c) 2015, 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 import java.lang.management.*;
       
    25 import java.util.*;
       
    26 import java.util.concurrent.*;
       
    27 import java.util.concurrent.atomic.*;
       
    28 
       
    29 /*
       
    30  * @test
       
    31  * @bug     6588467
       
    32  * @summary Basic test of ThreadInfo.isDaemon
       
    33  * @author  Jeremy Manson
       
    34  */
       
    35 public class ThreadDaemonTest {
       
    36 
       
    37     public static void main(String[] args) throws Exception {
       
    38         final int NUM_THREADS = 20;
       
    39         final String THREAD_PREFIX = "ThreadDaemonTest-";
       
    40 
       
    41         final CountDownLatch started = new CountDownLatch(NUM_THREADS);
       
    42         final CountDownLatch finished = new CountDownLatch(1);
       
    43         final AtomicReference<Exception> fail = new AtomicReference<>(null);
       
    44 
       
    45         Thread[] allThreads = new Thread[NUM_THREADS];
       
    46         ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
       
    47         Random rand = new Random();
       
    48 
       
    49         for (int i = 0; i < NUM_THREADS; i++) {
       
    50             allThreads[i] = new Thread(new Runnable() {
       
    51                     public void run() {
       
    52                         try {
       
    53                             started.countDown();
       
    54                             finished.await();
       
    55                         } catch (InterruptedException e) {
       
    56                             fail.set(new Exception(
       
    57                                 "Unexpected InterruptedException"));
       
    58                         }
       
    59                     }
       
    60                 }, THREAD_PREFIX + i);
       
    61             allThreads[i].setDaemon(rand.nextBoolean());
       
    62             allThreads[i].start();
       
    63         }
       
    64 
       
    65         started.await();
       
    66         try {
       
    67             ThreadInfo[] allThreadInfos = mbean.dumpAllThreads(false, false);
       
    68             int count = 0;
       
    69             for (int i = 0; i < allThreadInfos.length; i++) {
       
    70                 String threadName = allThreadInfos[i].getThreadName();
       
    71                 if (threadName.startsWith(THREAD_PREFIX)) {
       
    72                     count++;
       
    73                     String[] nameAndNumber = threadName.split("-");
       
    74                     int threadNum = Integer.parseInt(nameAndNumber[1]);
       
    75                     if (allThreads[threadNum].isDaemon() !=
       
    76                         allThreadInfos[i].isDaemon()) {
       
    77                         throw new RuntimeException(
       
    78                             allThreads[threadNum] + " is not like " +
       
    79                             allThreadInfos[i] + ". TEST FAILED.");
       
    80                     }
       
    81                 }
       
    82             }
       
    83             if (count != NUM_THREADS) {
       
    84                 throw new RuntimeException("Wrong number of threads examined");
       
    85             }
       
    86         }
       
    87         finally { finished.countDown(); }
       
    88 
       
    89         for (int i = 0; i < NUM_THREADS; i++) {
       
    90             allThreads[i].join();
       
    91         }
       
    92         if (fail.get() != null) {
       
    93             throw fail.get();
       
    94         }
       
    95     }
       
    96 }