jdk/test/java/lang/management/ThreadMXBean/AllThreadIds.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5808 3a1f603c5ca7
child 29495 e0d12d371446
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5808
diff changeset
     2
 * Copyright (c) 2003, 2010, 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 ThreadMXBean.getAllThreadIds()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Alexei Guibadoulline and Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build Barrier
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    31
 * @run main/othervm AllThreadIds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class AllThreadIds {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    final static int DAEMON_THREADS = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    final static int USER_THREADS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static volatile boolean live[] = new boolean[ALL_THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static Thread allThreads[] = new Thread[ALL_THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static ThreadMXBean mbean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static long prevTotalThreadCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static int prevLiveThreadCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static int prevPeakThreadCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static long curTotalThreadCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static int curLiveThreadCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static int curPeakThreadCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // barrier for threads communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static Barrier barrier = new Barrier(ALL_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static void printThreadList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (!trace) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        long[] list = mbean.getAllThreadIds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        for (int i = 1; i <= list.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println(i + ": Thread id = " + list[i-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        for (int i = 0; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            Thread t = allThreads[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            System.out.println(t.getName() + " Id = " + t.getId() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                " die = " + live[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                " alive = " + t.isAlive());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    73
    private static void fail(String msg) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    74
        trace = true;
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    75
        printThreadList();
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    76
        throw new RuntimeException(msg);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    77
    }
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    78
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static void checkThreadCount(int numNewThreads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                         int numTerminatedThreads)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        prevTotalThreadCount = curTotalThreadCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        prevLiveThreadCount = curLiveThreadCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        prevPeakThreadCount = curPeakThreadCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        curTotalThreadCount = mbean.getTotalStartedThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        curLiveThreadCount = mbean.getThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        curPeakThreadCount = mbean.getPeakThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if ((curLiveThreadCount - prevLiveThreadCount) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            (numNewThreads - numTerminatedThreads)) {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    91
            fail("Unexpected number of live threads: " +
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    92
                " Prev live = " + prevLiveThreadCount +
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    93
                " Current live = " + curLiveThreadCount +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                " Threads added = " + numNewThreads +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                " Threads terminated = " + numTerminatedThreads);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (curPeakThreadCount - prevPeakThreadCount != numNewThreads) {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    98
            fail("Unexpected number of peak threads: " +
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    99
                " Prev peak = " + prevPeakThreadCount +
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   100
                " Current peak = " + curPeakThreadCount +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                " Threads added = " + numNewThreads);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (curTotalThreadCount - prevTotalThreadCount != numNewThreads) {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   104
            fail("Unexpected number of total threads: " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                " Prev Total = " + prevTotalThreadCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                " Current Total = " + curTotalThreadCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                " Threads added = " + numNewThreads);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        long[] list = mbean.getAllThreadIds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (list.length != curLiveThreadCount) {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   111
            fail("Array length returned by " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                "getAllThreadIds() = " + list.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                " not matched count = " + curLiveThreadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        curTotalThreadCount = mbean.getTotalStartedThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        curLiveThreadCount = mbean.getThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        curPeakThreadCount = mbean.getPeakThreadCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        checkThreadCount(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Start all threads and wait to be sure they all are alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        barrier.set(ALL_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (int i = 0; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            live[i] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            allThreads[i] = new MyThread(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            allThreads[i].setDaemon( (i < DAEMON_THREADS) ? true : false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            allThreads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // wait until all threads are started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        checkThreadCount(ALL_THREADS, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        printThreadList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Check mbean now. All threads must appear in getAllThreadIds() list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        long[] list = mbean.getAllThreadIds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        for (int i = 0; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            long expectedId = allThreads[i].getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                System.out.print("Looking for thread with id " + expectedId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            for (int j = 0; j < list.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (expectedId == list[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    System.out.print(". TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Stop daemon threads, wait to be sure they all are dead, and check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // that they disappeared from getAllThreadIds() list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        barrier.set(DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (int i = 0; i < DAEMON_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            live[i] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // wait until daemon threads are terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // give chance to threads to terminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        pause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        checkThreadCount(0, DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // Check mbean now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        list = mbean.getAllThreadIds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        for (int i = 0; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            long expectedId = allThreads[i].getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            boolean live = (i >= DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                System.out.print("Looking for thread with id " + expectedId +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    (live ? " expected alive." : " expected terminated."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            for (int j = 0; j < list.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (expectedId == list[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (live != found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (live != found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    System.out.println(" TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // Stop all threads and wait to be sure they all are dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        barrier.set(ALL_THREADS - DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            live[i] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // wait until daemon threads are terminated .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // give chance to threads to terminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        pause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        checkThreadCount(0, ALL_THREADS - DAEMON_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // The MyThread thread lives as long as correspondent live[i] value is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private static class MyThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        MyThread(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            // signal started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            barrier.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            while (live[id]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    System.out.println("Unexpected exception is thrown.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // signal about to exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            barrier.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static Object pauseObj = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private static void pause() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // Enter lock a without blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        synchronized (pauseObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                // may need to tune this timeout for different platforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                pauseObj.wait(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                System.err.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                e.printStackTrace(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
}