jdk/test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java
author jbachorik
Thu, 05 Sep 2013 14:34:22 +0200
changeset 19814 77d0468c8874
parent 5506 202f599c92aa
child 22568 8871c88d0793
permissions -rw-r--r--
8004179: Few of test/java/lang/management/ThreadMXBean/* tests don't clean up the created threads Summary: Just run those tests in "othervm" mode. Reviewed-by: alanb, dfuchs, sjiang
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2005, 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 the synchronization statistics support:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
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
 * @ignore  6309226
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @build Semaphore
19814
77d0468c8874 8004179: Few of test/java/lang/management/ThreadMXBean/* tests don't clean up the created threads
jbachorik
parents: 5506
diff changeset
    33
 * @run main/othervm SynchronizationStatistics
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class SynchronizationStatistics {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static boolean blockedTimeCheck =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        mbean.isThreadContentionMonitoringSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static Object lockA = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static Object lockB = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static Object lockC = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static Object lockD = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static Object waiter = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static volatile boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static Object go = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static void goSleep(long ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            Thread.sleep(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (blockedTimeCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            mbean.setThreadContentionMonitoringEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (!mbean.isThreadContentionMonitoringEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                "Thread Contention Monitoring is not enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Examiner examiner = new Examiner("Examiner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        BlockedThread blocked = new BlockedThread("BlockedThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        examiner.setThread(blocked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // Start the threads and check them in  Blocked and Waiting states
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        examiner.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // wait until the examiner acquires all the locks and waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // for the BlockedThread to start
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        examiner.waitUntilWaiting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println("Checking the thread state for the examiner thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                           "is waiting to begin.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // The Examiner should be waiting to be notified by the BlockedThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        checkThreadState(examiner, Thread.State.WAITING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("Now starting the blocked thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        blocked.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            examiner.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            blocked.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static String INDENT = "    ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static void printStack(Thread t, StackTraceElement[] stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.out.println(INDENT +  t +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                           " stack: (length = " + stack.length + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            for (int j = 0; j < stack.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                System.out.println(INDENT + stack[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static void checkThreadState(Thread thread, Thread.State s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        ThreadInfo ti = mbean.getThreadInfo(thread.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (ti.getThreadState() != s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            ThreadInfo info = mbean.getThreadInfo(thread.getId(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                                  Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.out.println(INDENT + "TEST FAILED:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            printStack(thread, info.getStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println(INDENT + "Thread state: " + info.getThreadState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                "Thread state for " + thread + " returns " + ti.getThreadState() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                ".  Expected to be " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static void checkThreadState(Thread thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                         Thread.State s1, Thread.State s2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ThreadInfo ti = mbean.getThreadInfo(thread.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (ti.getThreadState() != s1 && ti.getThreadState() != s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                "Thread state for " + thread + " returns " + ti.getThreadState() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                ".  Expected to be " + s1 + " or " + s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    static class StatThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        private long blockingBaseTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        private long totalWaitTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        private long totalBlockedEnterTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        StatThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        void addWaitTime(long ns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            totalWaitTime = totalWaitTime + ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        void addBlockedEnterTime(long ns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            totalBlockedEnterTime = totalBlockedEnterTime + ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        void setBlockingBaseTime(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            blockingBaseTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        long totalBlockedTimeMs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return totalBlockedEnterTime / 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        long totalBlockedTimeMs(long now) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            long t = totalBlockedEnterTime + (now - blockingBaseTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return t / 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        long totalWaitTimeMs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return totalWaitTime / 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        long totalWaitTimeMs(long now) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            long t = totalWaitTime + (now - blockingBaseTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return t / 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static class BlockedThread extends StatThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        private Semaphore handshake = new Semaphore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        BlockedThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        void waitUntilBlocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            handshake.semaP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // give a chance for the examiner thread to really wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        void waitUntilWaiting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        boolean hasWaitersForBlocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return (handshake.getWaiterCount() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        private void notifyWaiter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // wait until the examiner waits on the semaphore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            while (handshake.getWaiterCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            handshake.semaV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        private void waitObj(long ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            synchronized (waiter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    // notify examinerabout to wait on a monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    long base = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    setBlockingBaseTime(base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    waiter.wait(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    addWaitTime(now - base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        private void test() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // notify examiner about to block on lockA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            long base = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            setBlockingBaseTime(base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            synchronized (lockA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                addBlockedEnterTime(now - base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                A(); // Expected blocked count = 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            E();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        private void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // notify examiner about to block on lockB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            long base = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            setBlockingBaseTime(base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            synchronized (lockB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                addBlockedEnterTime(now - base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                B(); // Expected blocked count = 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        private void B() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // notify examiner about to block on lockC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            long base = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            setBlockingBaseTime(base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            synchronized (lockC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                addBlockedEnterTime(now - base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                C();  // Expected blocked count = 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        private void C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            // notify examiner about to block on lockD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            notifyWaiter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            long base = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            setBlockingBaseTime(base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            synchronized (lockD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                addBlockedEnterTime(now - base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                D();  // Expected blocked count = 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        private void D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            goSleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        private void E() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            final int WAIT = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            waitObj(WAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            waitObj(WAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            waitObj(WAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } // run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    } // BlockedThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    static int blockedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    static int waitedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    static class Examiner extends StatThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        private BlockedThread blockedThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        private Semaphore semaphore = new Semaphore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Examiner(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        public void setThread(BlockedThread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            blockedThread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        private void blockedTimeRangeCheck(StatThread t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                           long blockedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                           long nowNano)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            long expected = t.totalBlockedTimeMs(nowNano);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            // accept 5% range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            timeRangeCheck(blockedTime, expected, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        private void waitedTimeRangeCheck(StatThread t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                          long waitedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                          long nowNano)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            long expected = t.totalWaitTimeMs(nowNano);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            // accept 5% range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            timeRangeCheck(waitedTime, expected, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        private void timeRangeCheck(long time, long expected, int percent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            double diff = expected - time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                 System.out.println("  Time = " + time +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    " expected = " + expected +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    ".  Diff = " + diff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            // throw an exception if blockedTime and expectedTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // differs > percent%
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (diff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                diff = diff * -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            long range = (expected * percent) / 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // minimum range = 2 ms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (range < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                range = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (diff > range) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    "Time returned = " + time +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    " expected = " + expected + ".  Diff = " + diff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        private void checkInfo(StatThread t, Thread.State s, Object lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                               String lockName, int bcount, int wcount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            String action = "ERROR";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (s == Thread.State.WAITING || s == Thread.State.TIMED_WAITING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                action = "wait on ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } else if (s == Thread.State.BLOCKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                action = "block on ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            System.out.println(t + " expected to " + action + lockName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                " with blocked count = " + bcount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                " and waited count = " + wcount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            long now = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            ThreadInfo info = mbean.getThreadInfo(t.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (info.getThreadState() != s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                printStack(t, info.getStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    "Thread state returned is " + info.getThreadState() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    ". Expected to be " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (info.getLockName() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                !info.getLockName().equals(lock.toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    "getLockName() returned " + info.getLockName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    ". Expected to be " + lockName + " - "  + lock.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (info.getBlockedCount() != bcount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    "Blocked Count returned is " + info.getBlockedCount() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    ". Expected to be " + bcount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (info.getWaitedCount() != wcount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    "Waited Count returned is " + info.getWaitedCount() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    ". Expected to be " + wcount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            String lockObj = info.getLockName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (lockObj == null || !lockObj.equals(lock.toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    "Object blocked on is " + lockObj  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    ". Expected to be " + lock.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (!blockedTimeCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            long blockedTime = info.getBlockedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (blockedTime < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    "Blocked time returned is negative = " + blockedTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (s == Thread.State.BLOCKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                blockedTimeRangeCheck(t, blockedTime, now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                timeRangeCheck(blockedTime, t.totalBlockedTimeMs(), 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            long waitedTime = info.getWaitedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (waitedTime < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    "Waited time returned is negative = " + waitedTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (s == Thread.State.WAITING || s == Thread.State.TIMED_WAITING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                waitedTimeRangeCheck(t, waitedTime, now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                timeRangeCheck(waitedTime, t.totalWaitTimeMs(), 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        private void examine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                synchronized (lockD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    synchronized (lockC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        synchronized (lockB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                            synchronized (lockA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                // notify main thread to continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                semaphore.semaV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                // wait until BlockedThread has started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                blockedThread.waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                blockedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                checkInfo(blockedThread, Thread.State.BLOCKED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                          lockA, "lockA",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                          blockedCount, waitedCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                           // wait until BlockedThread to block on lockB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            blockedThread.waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                            blockedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            checkInfo(blockedThread, Thread.State.BLOCKED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                      lockB, "lockB",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                      blockedCount, waitedCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        // wait until BlockedThread to block on lockC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        blockedThread.waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        blockedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        checkInfo(blockedThread, Thread.State.BLOCKED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                  lockC, "lockC",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                  blockedCount, waitedCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    // wait until BlockedThread to block on lockD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    blockedThread.waitUntilBlocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    blockedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    checkInfo(blockedThread, Thread.State.BLOCKED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                              lockD, "lockD",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                              blockedCount, waitedCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                // wait until BlockedThread about to call E()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                // BlockedThread will wait on waiter for 3 times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                blockedThread.waitUntilWaiting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                waitedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                checkInfo(blockedThread, Thread.State.TIMED_WAITING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                          waiter, "waiter", blockedCount, waitedCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                blockedThread.waitUntilWaiting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                waitedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                checkInfo(blockedThread, Thread.State.TIMED_WAITING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                          waiter, "waiter", blockedCount, waitedCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                blockedThread.waitUntilWaiting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                waitedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                checkInfo(blockedThread, Thread.State.TIMED_WAITING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                          waiter, "waiter", blockedCount, waitedCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            examine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        } // run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        public void waitUntilWaiting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            semaphore.semaP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            // wait until the examiner is waiting for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            while (!blockedThread.hasWaitersForBlocked()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                goSleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            // give a chance for the examiner thread to really wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            goSleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    } // Examiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
}