Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ThreadDaemonTest.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.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 it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.management.*;24import java.util.*;25import java.util.concurrent.*;26import java.util.concurrent.atomic.*;2728/*29* @test30* @bug 658846731* @summary Basic test of ThreadInfo.isDaemon32* @author Jeremy Manson33*/34public class ThreadDaemonTest {3536public static void main(String[] args) throws Exception {37final int NUM_THREADS = 20;38final String THREAD_PREFIX = "ThreadDaemonTest-";3940final CountDownLatch started = new CountDownLatch(NUM_THREADS);41final CountDownLatch finished = new CountDownLatch(1);42final AtomicReference<Exception> fail = new AtomicReference<>(null);4344Thread[] allThreads = new Thread[NUM_THREADS];45ThreadMXBean mbean = ManagementFactory.getThreadMXBean();46Random rand = new Random();4748for (int i = 0; i < NUM_THREADS; i++) {49allThreads[i] = new Thread(new Runnable() {50public void run() {51try {52started.countDown();53finished.await();54} catch (InterruptedException e) {55fail.set(new Exception(56"Unexpected InterruptedException"));57}58}59}, THREAD_PREFIX + i);60allThreads[i].setDaemon(rand.nextBoolean());61allThreads[i].start();62}6364started.await();65try {66ThreadInfo[] allThreadInfos = mbean.dumpAllThreads(false, false);67int count = 0;68for (int i = 0; i < allThreadInfos.length; i++) {69String threadName = allThreadInfos[i].getThreadName();70if (threadName.startsWith(THREAD_PREFIX)) {71count++;72String[] nameAndNumber = threadName.split("-");73int threadNum = Integer.parseInt(nameAndNumber[1]);74if (allThreads[threadNum].isDaemon() !=75allThreadInfos[i].isDaemon()) {76throw new RuntimeException(77allThreads[threadNum] + " is not like " +78allThreadInfos[i] + ". TEST FAILED.");79}80}81}82if (count != NUM_THREADS) {83throw new RuntimeException("Wrong number of threads examined");84}85}86finally { finished.countDown(); }8788for (int i = 0; i < NUM_THREADS; i++) {89allThreads[i].join();90}91if (fail.get() != null) {92throw fail.get();93}94}95}969798