jdk/test/java/lang/management/ThreadMXBean/ThreadCounts.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
child 32649 2ee9017c7597
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
 *
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    33
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main ThreadCounts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ThreadCounts {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    final static int DAEMON_THREADS = 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    final static int USER_THREADS_1 = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    final static int USER_THREADS_2 = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    final static int USER_THREADS = USER_THREADS_1 + USER_THREADS_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static volatile boolean live[] = new boolean[ALL_THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // barrier for threads communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static Barrier barrier = new Barrier(DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void main(String argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        ThreadCounts test = new ThreadCounts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Thread allThreads[] = new Thread[ALL_THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // Start DAEMON_THREADS threads and wait to be sure they all are alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        barrier.set(DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        for (int i = 0; i < DAEMON_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            live[i] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            allThreads[i] = new MyThread(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            allThreads[i].setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            allThreads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // wait until all threads have started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.out.println("Number of daemon threads added = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                           DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if ( (!test.checkCount  (DAEMON_THREADS)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
             (!test.checkCreated(DAEMON_THREADS)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             (!test.checkPeak   (DAEMON_THREADS)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
             (!test.checkDaemon (DAEMON_THREADS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // Start USER_THREADS_1 threads and wait to be sure they all are alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        barrier.set(USER_THREADS_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        for (int i = DAEMON_THREADS; i < DAEMON_THREADS + USER_THREADS_1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            live[i] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            allThreads[i] = new MyThread(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            allThreads[i].setDaemon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            allThreads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // wait until user1 threads have started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println("Number of threads added = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                           USER_THREADS_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if ( (!test.checkCount  (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
             (!test.checkCreated(DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
             (!test.checkDaemon (DAEMON_THREADS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // Stop daemon threads and wait to be sure they all are dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        barrier.set(DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        for (int i = 0; i < DAEMON_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            live[i] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // wait until daemon threads terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println("Daemon threads terminated.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if ( (!test.checkCount  (USER_THREADS_1))                  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             (!test.checkCreated(DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             (!test.checkDaemon (0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Start USER_THREADS_2 threads and wait to be sure they all are alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        barrier.set(USER_THREADS_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (int i = DAEMON_THREADS + USER_THREADS_1; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            live[i] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            allThreads[i] = new MyThread(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            allThreads[i].setDaemon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            allThreads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // wait until user2 threads have started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println("Number of threads added = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                           USER_THREADS_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if ( (!test.checkCount  (USER_THREADS_1 + USER_THREADS_2)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             (!test.checkCreated(ALL_THREADS))                     ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             (!test.checkDaemon (0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // Stop user1 threads and wait to be sure they all are dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        barrier.set(USER_THREADS_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (int i = DAEMON_THREADS; i < DAEMON_THREADS + USER_THREADS_1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            live[i] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // wait until user1 threads terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        System.out.println("Number of threads terminated = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                           USER_THREADS_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if ( (!test.checkCount  (USER_THREADS_2))                  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
             (!test.checkCreated(ALL_THREADS))                     ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
             (!test.checkDaemon (0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // Stop user2 threads and wait to be sure they all are dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        barrier.set(USER_THREADS_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        for (int i = DAEMON_THREADS + USER_THREADS_1; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            live[i] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // wait until user2 threads terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        System.out.println("Number of threads terminated = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                           USER_THREADS_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if ( (!test.checkCount  (0))                               ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
             (!test.checkCreated(ALL_THREADS))                     ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             (!test.checkPeak   (DAEMON_THREADS + USER_THREADS_1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
             (!test.checkDaemon (0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
           )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // Nobody knows how many threads are in the JVM in the exact moment. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // only thing to check for sure is minimal number of threads alive (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // created) in the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private boolean checkCount(long min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        long result = mbean.getThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (result < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            System.err.println("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                               "Minimal number of live threads is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                min +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                ". ThreadMXBean.getThreadCount() returned " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private boolean checkCreated(long min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        long result = mbean.getTotalStartedThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (result < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            System.err.println("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                               "Minimal number of created threads is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                min +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                ". ThreadMXBean.getTotalStartedThreadCount() "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                "returned " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private boolean checkPeak(long min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        long result = mbean.getPeakThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (result < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            System.err.println("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                               "Minimal peak thread count is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                min +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                ". ThreadMXBean.getPeakThreadCount() "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                "returned " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private boolean checkDaemon(long min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        long result = mbean.getDaemonThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (result < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            System.err.println("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                               "Minimal number of daemon thread count is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                min +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                               "ThreadMXBean.getDaemonThreadCount() returned "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // The MyThread thread lives as long as correspondent live[i] value is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static class MyThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        MyThread(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // signal started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            barrier.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            while (live[id]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    System.out.println("Unexpected exception is thrown.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // signal about to exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            barrier.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
}