jdk/test/java/lang/management/ThreadMXBean/ThreadCounts.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4530538
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic unit test of mbean.getThreadCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *                             mbean.getTotalStartedThreadCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *                             mbean.getPeakThreadCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *                             mbean.getDaemonThreadCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author  Alexei Guibadoulline
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main ThreadCounts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class ThreadCounts {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    final static int DAEMON_THREADS = 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    final static int USER_THREADS_1 = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    final static int USER_THREADS_2 = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    final static int USER_THREADS = USER_THREADS_1 + USER_THREADS_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static volatile boolean live[] = new boolean[ALL_THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // barrier for threads communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static Barrier barrier = new Barrier(DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static void main(String argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ThreadCounts test = new ThreadCounts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Thread allThreads[] = new Thread[ALL_THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // Start DAEMON_THREADS threads and wait to be sure they all are alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        barrier.set(DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        for (int i = 0; i < DAEMON_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            live[i] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            allThreads[i] = new MyThread(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            allThreads[i].setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            allThreads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // wait until all threads have started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        System.out.println("Number of daemon threads added = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                           DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if ( (!test.checkCount  (DAEMON_THREADS)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
             (!test.checkCreated(DAEMON_THREADS)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
             (!test.checkPeak   (DAEMON_THREADS)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             (!test.checkDaemon (DAEMON_THREADS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // Start USER_THREADS_1 threads and wait to be sure they all are alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        barrier.set(USER_THREADS_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        for (int i = DAEMON_THREADS; i < DAEMON_THREADS + USER_THREADS_1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            live[i] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            allThreads[i] = new MyThread(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            allThreads[i].setDaemon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            allThreads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // wait until user1 threads have started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        System.out.println("Number of threads added = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                           USER_THREADS_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if ( (!test.checkCount  (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             (!test.checkCreated(DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             (!test.checkDaemon (DAEMON_THREADS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // Stop daemon threads and wait to be sure they all are dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        barrier.set(DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        for (int i = 0; i < DAEMON_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            live[i] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // wait until daemon threads terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        System.out.println("Daemon threads terminated.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if ( (!test.checkCount  (USER_THREADS_1))                  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             (!test.checkCreated(DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             (!test.checkDaemon (0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Start USER_THREADS_2 threads and wait to be sure they all are alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        barrier.set(USER_THREADS_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        for (int i = DAEMON_THREADS + USER_THREADS_1; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            live[i] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            allThreads[i] = new MyThread(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            allThreads[i].setDaemon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            allThreads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // wait until user2 threads have started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.println("Number of threads added = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                           USER_THREADS_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if ( (!test.checkCount  (USER_THREADS_1 + USER_THREADS_2)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             (!test.checkCreated(ALL_THREADS))                     ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             (!test.checkDaemon (0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // Stop user1 threads and wait to be sure they all are dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        barrier.set(USER_THREADS_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        for (int i = DAEMON_THREADS; i < DAEMON_THREADS + USER_THREADS_1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            live[i] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // wait until user1 threads terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.out.println("Number of threads terminated = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                           USER_THREADS_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if ( (!test.checkCount  (USER_THREADS_2))                  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
             (!test.checkCreated(ALL_THREADS))                     ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
             (!test.checkDaemon (0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // Stop user2 threads and wait to be sure they all are dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        barrier.set(USER_THREADS_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        for (int i = DAEMON_THREADS + USER_THREADS_1; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            live[i] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // wait until user2 threads terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        System.out.println("Number of threads terminated = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                           USER_THREADS_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if ( (!test.checkCount  (0))                               ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
             (!test.checkCreated(ALL_THREADS))                     ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             (!test.checkDaemon (0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // Nobody knows how many threads are in the JVM in the exact moment. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // only thing to check for sure is minimal number of threads alive (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // created) in the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private boolean checkCount(long min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        long result = mbean.getThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (result < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            System.err.println("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                               "Minimal number of live threads is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                min +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                ". ThreadMXBean.getThreadCount() returned " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private boolean checkCreated(long min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        long result = mbean.getTotalStartedThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (result < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            System.err.println("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                               "Minimal number of created threads is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                min +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                ". ThreadMXBean.getTotalStartedThreadCount() "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                "returned " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private boolean checkPeak(long min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        long result = mbean.getPeakThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (result < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            System.err.println("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                               "Minimal peak thread count is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                min +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                ". ThreadMXBean.getPeakThreadCount() "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                "returned " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private boolean checkDaemon(long min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        long result = mbean.getDaemonThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (result < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            System.err.println("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                               "Minimal number of daemon thread count is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                min +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                               "ThreadMXBean.getDaemonThreadCount() returned "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // The MyThread thread lives as long as correspondent live[i] value is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private static class MyThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        MyThread(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // signal started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            barrier.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            while (live[id]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    System.out.println("Unexpected exception is thrown.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // signal about to exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            barrier.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
}