jdk/test/java/lang/management/ThreadMXBean/ThreadStackTrace.java
author mchung
Tue, 26 May 2009 17:54:28 -0700
changeset 2931 27a20b9d488e
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status. Summary: remove @ignore Reviewed-by: swamyv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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 ThreadInfo.getStackTrace() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          ThreadInfo.getThreadState()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
    31
 * @run build Semaphore Utils
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main ThreadStackTrace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ThreadStackTrace {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static ThreadMXBean mbean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static boolean notified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static Object lockA = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static Object lockB = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static volatile boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static String[] blockedStack = {"run", "test", "A", "B", "C", "D"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static int bsDepth = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static int methodB = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static String[] examinerStack = {"run", "examine1", "examine2"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static int esDepth = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static int methodExamine1= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static void checkNullThreadInfo(Thread t) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ThreadInfo ti = mbean.getThreadInfo(t.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (ti != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            ThreadInfo info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            System.out.println(INDENT + "TEST FAILED:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                printStack(t, info.getStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                System.out.println(INDENT + "Thread state: " + info.getThreadState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                "getThreadInfo() is expected to return null for " + t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Examiner examiner = new Examiner("Examiner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        BlockedThread blocked = new BlockedThread("BlockedThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        examiner.setThread(blocked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        checkNullThreadInfo(examiner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        checkNullThreadInfo(blocked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Start the threads and check them in  Blocked and Waiting states
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        examiner.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // block until examiner begins doing its real work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        examiner.waitForStarted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println("Checking stack trace for the examiner thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                           "is waiting to begin.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // The Examiner should be waiting to be notified by the BlockedThread
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
    89
        Utils.checkThreadState(examiner, Thread.State.WAITING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // Check that the stack is returned correctly for a new thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        checkStack(examiner, examinerStack, esDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("Now starting the blocked thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        blocked.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            examiner.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            blocked.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Check that the stack is returned correctly for a terminated thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        checkNullThreadInfo(examiner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        checkNullThreadInfo(blocked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static String INDENT = "    ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static void printStack(Thread t, StackTraceElement[] stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        System.out.println(INDENT +  t +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                           " stack: (length = " + stack.length + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            for (int j = 0; j < stack.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                System.out.println(INDENT + stack[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static void checkStack(Thread t, String[] expectedStack,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                   int depth) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        ThreadInfo ti = mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        StackTraceElement[] stack = ti.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            printStack(t, stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int frame = stack.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (int i = 0; i < depth; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (! stack[frame].getMethodName().equals(expectedStack[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    "Expected " + expectedStack[i] + " in frame " + frame +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    " but got " + stack[frame].getMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            frame--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static class BlockedThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        private Semaphore handshake = new Semaphore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        BlockedThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        boolean hasWaitersForBlocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return (handshake.getWaiterCount() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        void waitUntilBlocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            handshake.semaP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // give a chance for the examiner thread to really wait
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   161
            Utils.goSleep(20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        void waitUntilLockAReleased() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            handshake.semaP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // give a chance for the examiner thread to really wait
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   168
            Utils.goSleep(50);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        private void notifyWaiter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // wait until the examiner waits on the semaphore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            while (handshake.getWaiterCount() == 0) {
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   174
                Utils.goSleep(20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            handshake.semaV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        private void test() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        private void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            B();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        private void B() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // notify the examiner about to block on lockB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            synchronized (lockB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        private void C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        private void D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // Notify that examiner about to enter lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            synchronized (lockA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                notified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                while (!notified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        // notify the examiner about to release lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        // Wait and let examiner thread check the mbean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        lockA.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                System.out.println("BlockedThread notified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } // run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    } // BlockedThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static class Examiner extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        private static BlockedThread blockedThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        private Semaphore handshake = new Semaphore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        Examiner(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        public void setThread(BlockedThread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            blockedThread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public synchronized void waitForStarted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // wait until the examiner is about to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            handshake.semaP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // wait until the examiner is waiting for blockedThread's notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            while (!blockedThread.hasWaitersForBlocked()) {
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   242
                Utils.goSleep(50);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // give a chance for the examiner thread to really wait
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   245
            Utils.goSleep(20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        private Thread itself;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        private void examine1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            synchronized (lockB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                examine2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    System.out.println("Checking examiner's its own stack trace");
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   254
                    Utils.checkThreadState(itself, Thread.State.RUNNABLE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    checkStack(itself, examinerStack, methodExamine1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    // wait until blockedThread is blocked on lockB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    blockedThread.waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    System.out.println("Checking stack trace for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        "BlockedThread - should be blocked on lockB.");
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   262
                    Utils.checkThreadState(blockedThread, Thread.State.BLOCKED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    checkStack(blockedThread, blockedStack, methodB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        private void examine2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            synchronized (lockA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                // wait until main thread gets signalled of the semaphore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                while (handshake.getWaiterCount() == 0) {
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   276
                    Utils.goSleep(20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                handshake.semaV();  // notify the main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    // Wait until BlockedThread is about to block on lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    blockedThread.waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    System.out.println("Checking examiner's its own stack trace");
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   285
                    Utils.checkThreadState(itself, Thread.State.RUNNABLE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    checkStack(itself, examinerStack, esDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    System.out.println("Checking stack trace for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        "BlockedThread - should be blocked on lockA.");
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   290
                    Utils.checkThreadState(blockedThread, Thread.State.BLOCKED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    checkStack(blockedThread, blockedStack, bsDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // release lockA and let BlockedThread to get the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // and wait on lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            blockedThread.waitUntilLockAReleased();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            synchronized (lockA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    System.out.println("Checking stack trace for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        "BlockedThread - should be waiting on lockA.");
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   308
                    Utils.checkThreadState(blockedThread, Thread.State.WAITING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    checkStack(blockedThread, blockedStack, bsDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    // Let the blocked thread go
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    notified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    lockA.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            // give some time for BlockedThread to proceed
2931
27a20b9d488e 6798842: TEST_BUG: ThreadStackTrace.java fails intermittently with unexpected thread status.
mchung
parents: 2
diff changeset
   321
            Utils.goSleep(50);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } // examine2()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            itself = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            examine1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } // run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    } // Examiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
}