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  * @modules java.management
  35  */
  36 public class ThreadDaemonTest {
  37 
  38     public static void main(String[] args) throws Exception {
  39         final int NUM_THREADS = 20;
  40         final String THREAD_PREFIX = "ThreadDaemonTest-";
  41 
  42         final CountDownLatch started = new CountDownLatch(NUM_THREADS);
  43         final CountDownLatch finished = new CountDownLatch(1);
  44         final AtomicReference<Exception> fail = new AtomicReference<>(null);
  45 
  46         Thread[] allThreads = new Thread[NUM_THREADS];
  47         ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
  48         Random rand = new Random();
  49 
  50         for (int i = 0; i < NUM_THREADS; i++) {
  51             allThreads[i] = new Thread(new Runnable() {
  52                     public void run() {
  53                         try {
  54                             started.countDown();
  55                             finished.await();
  56                         } catch (InterruptedException e) {
  57                             fail.set(new Exception(
  58                                 "Unexpected InterruptedException"));
  59                         }
  60                     }
  61                 }, THREAD_PREFIX + i);
  62             allThreads[i].setDaemon(rand.nextBoolean());
  63             allThreads[i].start();
  64         }
  65 
  66         started.await();
  67         try {
  68             ThreadInfo[] allThreadInfos = mbean.dumpAllThreads(false, false);
  69             int count = 0;
  70             for (int i = 0; i < allThreadInfos.length; i++) {
  71                 String threadName = allThreadInfos[i].getThreadName();
  72                 if (threadName.startsWith(THREAD_PREFIX)) {
  73                     count++;
  74                     String[] nameAndNumber = threadName.split("-");
  75                     int threadNum = Integer.parseInt(nameAndNumber[1]);
  76                     if (allThreads[threadNum].isDaemon() !=
  77                         allThreadInfos[i].isDaemon()) {
  78                         throw new RuntimeException(
  79                             allThreads[threadNum] + " is not like " +
  80                             allThreadInfos[i] + ". TEST FAILED.");
  81                     }
  82                 }
  83             }
  84             if (count != NUM_THREADS) {
  85                 throw new RuntimeException("Wrong number of threads examined");
  86             }
  87         }
  88         finally { finished.countDown(); }
  89 
  90         for (int i = 0; i < NUM_THREADS; i++) {
  91             allThreads[i].join();
  92         }
  93         if (fail.get() != null) {
  94             throw fail.get();
  95         }
  96     }
  97 }