jdk/test/java/lang/management/ThreadMXBean/ThreadStackTrace.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2931 27a20b9d488e
permissions -rw-r--r--
Initial load
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build Semaphore
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 goSleep(long ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            Thread.sleep(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static void checkNullThreadInfo(Thread t) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ThreadInfo ti = mbean.getThreadInfo(t.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (ti != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            ThreadInfo info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            System.out.println(INDENT + "TEST FAILED:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                printStack(t, info.getStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                System.out.println(INDENT + "Thread state: " + info.getThreadState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                "getThreadInfo() is expected to return null for " + t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Examiner examiner = new Examiner("Examiner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        BlockedThread blocked = new BlockedThread("BlockedThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        examiner.setThread(blocked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        checkNullThreadInfo(examiner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        checkNullThreadInfo(blocked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // Start the threads and check them in  Blocked and Waiting states
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        examiner.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // block until examiner begins doing its real work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        examiner.waitForStarted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("Checking stack trace for the examiner thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                           "is waiting to begin.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // The Examiner should be waiting to be notified by the BlockedThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        checkThreadState(examiner, Thread.State.WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Check that the stack is returned correctly for a new thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        checkStack(examiner, examinerStack, esDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        System.out.println("Now starting the blocked thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        blocked.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            examiner.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            blocked.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println("Unexpected exception.");
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Check that the stack is returned correctly for a terminated thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        checkNullThreadInfo(examiner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        checkNullThreadInfo(blocked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static String INDENT = "    ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static void printStack(Thread t, StackTraceElement[] stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        System.out.println(INDENT +  t +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                           " stack: (length = " + stack.length + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            for (int j = 0; j < stack.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                System.out.println(INDENT + stack[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static void checkThreadState(Thread thread, Thread.State s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        ThreadInfo ti = mbean.getThreadInfo(thread.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (ti.getThreadState() != s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            ThreadInfo info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                mbean.getThreadInfo(thread.getId(), Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.out.println(INDENT + "TEST FAILED:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            printStack(thread, info.getStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.out.println(INDENT + "Thread state: " + info.getThreadState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                "Thread state for " + thread + " returns " + ti.getThreadState() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                ".  Expected to be " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static void checkThreadState(Thread thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                         Thread.State s1, Thread.State s2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ThreadInfo ti = mbean.getThreadInfo(thread.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (ti.getThreadState() != s1 && ti.getThreadState() != s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                "Thread state for " + thread + " returns " + ti.getThreadState() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                ".  Expected to be " + s1 + " or " + s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static void checkStack(Thread t, String[] expectedStack,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                   int depth) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        ThreadInfo ti = mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        StackTraceElement[] stack = ti.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            printStack(t, stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        int frame = stack.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (int i = 0; i < depth; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (! stack[frame].getMethodName().equals(expectedStack[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    "Expected " + expectedStack[i] + " in frame " + frame +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    " but got " + stack[frame].getMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            frame--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static class BlockedThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        private Semaphore handshake = new Semaphore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        BlockedThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        boolean hasWaitersForBlocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return (handshake.getWaiterCount() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        void waitUntilBlocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            handshake.semaP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // give a chance for the examiner thread to really wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        void waitUntilLockAReleased() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            handshake.semaP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // give a chance for the examiner thread to really wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            goSleep(50);
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 void notifyWaiter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // wait until the examiner waits on the semaphore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            while (handshake.getWaiterCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            handshake.semaV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        private void test() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        private void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            B();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        private void B() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            // notify the examiner about to block on lockB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            synchronized (lockB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        private void C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        private void D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // Notify that examiner about to enter lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            synchronized (lockA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                notified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                while (!notified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        // notify the examiner about to release lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        // Wait and let examiner thread check the mbean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        lockA.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                System.out.println("BlockedThread notified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } // run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    } // BlockedThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    static class Examiner extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        private static BlockedThread blockedThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        private Semaphore handshake = new Semaphore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        Examiner(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public void setThread(BlockedThread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            blockedThread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        public synchronized void waitForStarted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // wait until the examiner is about to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            handshake.semaP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // wait until the examiner is waiting for blockedThread's notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            while (!blockedThread.hasWaitersForBlocked()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                goSleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // give a chance for the examiner thread to really wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        private Thread itself;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        private void examine1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            synchronized (lockB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                examine2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    System.out.println("Checking examiner's its own stack trace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    checkThreadState(itself, Thread.State.RUNNABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    checkStack(itself, examinerStack, methodExamine1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    // wait until blockedThread is blocked on lockB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    blockedThread.waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    System.out.println("Checking stack trace for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        "BlockedThread - should be blocked on lockB.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    checkThreadState(blockedThread, Thread.State.BLOCKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    checkStack(blockedThread, blockedStack, methodB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        private void examine2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            synchronized (lockA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                // wait until main thread gets signalled of the semaphore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                while (handshake.getWaiterCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                handshake.semaV();  // notify the main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    // Wait until BlockedThread is about to block on lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    blockedThread.waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    System.out.println("Checking examiner's its own stack trace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    checkThreadState(itself, Thread.State.RUNNABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    checkStack(itself, examinerStack, esDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    System.out.println("Checking stack trace for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        "BlockedThread - should be blocked on lockA.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    checkThreadState(blockedThread, Thread.State.BLOCKED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    checkStack(blockedThread, blockedStack, bsDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // release lockA and let BlockedThread to get the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // and wait on lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            blockedThread.waitUntilLockAReleased();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            synchronized (lockA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    System.out.println("Checking stack trace for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        "BlockedThread - should be waiting on lockA.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    checkThreadState(blockedThread, Thread.State.WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    checkStack(blockedThread, blockedStack, bsDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    // Let the blocked thread go
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    notified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    lockA.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // give some time for BlockedThread to proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            goSleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } // examine2()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            itself = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            examine1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } // run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    } // Examiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
}