jdk/test/java/lang/management/ThreadMXBean/LockingThread.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2933
diff changeset
     2
 * Copyright (c) 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: 2933
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2933
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2933
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
 * @bug     5086470  6358247
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary LockingThread is used by LockedMonitors test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          It will create threads that have:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          - a stack frame acquires no monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          - a stack frame acquires one or more monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          - a stack frame blocks on Object.wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *            and the monitor waiting is not locked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @build Barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @build ThreadDump
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class LockingThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static Lock lock1 = new Lock("lock1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static Lock lock2 = new Lock("lock2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static Lock lock3 = new Lock("lock3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static Lock lock4 = new Lock("lock4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static Lock lock5 = new Lock("lock5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static Lock lock6 = new Lock("lock6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static Lock lock7 = new Lock("lock7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static Lock lock8 = new Lock("lock8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static LockingThread t1 = new Thread1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static LockingThread t2 = new Thread2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static Barrier barr = new Barrier(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static int count = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static void startLockingThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        t1.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        t2.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        t1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        t2.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // wait until t1 waits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        while (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
               Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
           } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
               throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
2933
08ea3ecb912c 6512493: TEST_BUG: unexpected LockInfo failure in LockedSynchronizers.java
mchung
parents: 2
diff changeset
    69
        Utils.waitForBlockWaitingState(t1);
08ea3ecb912c 6512493: TEST_BUG: unexpected LockInfo failure in LockedSynchronizers.java
mchung
parents: 2
diff changeset
    70
        Utils.waitForBlockWaitingState(t2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static long[] getThreadIds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return new long[] {t1.getId(), t2.getId()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void checkLockedMonitors(ThreadInfo[] tinfos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int matches = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        for (ThreadInfo ti : tinfos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (ti.getThreadId() == t1.getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                t1.checkLockedMonitors(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (ti.getThreadId() == t2.getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                t2.checkLockedMonitors(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (matches != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new RuntimeException("MonitorInfo missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static class Lock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Lock(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    final String threadName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    Lock         waitingLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    int          numOwnedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    Map<String, Lock[]> ownedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public LockingThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.threadName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    protected void setExpectedResult(Lock waitingLock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                     int numOwnedMonitors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                     Map<String, Lock[]> ownedMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.waitingLock = waitingLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.numOwnedMonitors = numOwnedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.ownedMonitors = ownedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    void checkLockedMonitors(ThreadInfo info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        checkThreadInfo(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        MonitorInfo[] monitors = info.getLockedMonitors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (monitors.length != numOwnedMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            ThreadDump.threadDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new RuntimeException("Number of locked monitors = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                monitors.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                " not matched. Expected: " + numOwnedMonitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // check if each monitor returned in the list is the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        for (MonitorInfo m : monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            StackTraceElement ste = m.getLockedStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int depth = m.getLockedStackDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            checkStackFrame(info, ste, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            checkMonitor(m, ste.getMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // check if each expected monitor is included in the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        for (Map.Entry<String, Lock[]> e : ownedMonitors.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            for (Lock l : e.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                checkMonitor(e.getKey(), l, monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (info.getLockedSynchronizers().length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            ThreadDump.threadDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new RuntimeException("Number of locked synchronizers = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                info.getLockedSynchronizers().length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                " not matched. Expected: 0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    void checkThreadInfo(ThreadInfo info) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (!getName().equals(info.getThreadName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new RuntimeException("Name: " + info.getThreadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                " not matched. Expected: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        LockInfo l = info.getLockInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if ((waitingLock == null && l != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            (waitingLock != null && l == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new RuntimeException("LockInfo: " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                " not matched. Expected: " + waitingLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        String waitingLockName = waitingLock.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int hcode = System.identityHashCode(waitingLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (!waitingLockName.equals(l.getClassName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new RuntimeException("LockInfo : " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                " class name not matched. Expected: " + waitingLockName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (hcode != l.getIdentityHashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new RuntimeException("LockInfo: " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                " IdentityHashCode not matched. Expected: " + hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        String lockName = info.getLockName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        String[] s = lockName.split("@");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (!waitingLockName.equals(s[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new RuntimeException("LockName: " + lockName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                " class name not matched. Expected: " + waitingLockName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int i = Integer.parseInt(s[1], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (hcode != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new RuntimeException("LockName: " + lockName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                " IdentityHashCode not matched. Expected: " + hcode);
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
    void checkStackFrame(ThreadInfo info, StackTraceElement ste, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        StackTraceElement[] stacktrace = info.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (!ste.equals(stacktrace[depth])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            System.out.println("LockedStackFrame:- " + ste);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.out.println("StackTrace at " + depth + " :-" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                stacktrace[depth]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new RuntimeException("LockedStackFrame does not match " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                "stack frame in ThreadInfo.getStackTrace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    void checkMonitor(MonitorInfo m, String methodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        for (Map.Entry<String, Lock[]> e : ownedMonitors.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (methodName.equals(e.getKey())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                for (Lock l : e.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    String className = l.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    int hcode = System.identityHashCode(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    if (className.equals(m.getClassName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        hcode == m.getIdentityHashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        // monitor matched the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        throw new RuntimeException("Monitor not expected" + m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    void checkMonitor(String methodName, Lock l, MonitorInfo[] monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        String className = l.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int hcode = System.identityHashCode(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (MonitorInfo m : monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (className.equals(m.getClassName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                hcode == m.getIdentityHashCode() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                methodName.equals(m.getLockedStackFrame().getMethodName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throw new RuntimeException("Monitor not found in the returned list" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            " Method: " + methodName + " Lock: " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    static class Thread1 extends LockingThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public Thread1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            super("t1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            initExpectedResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            synchronized(lock1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                synchronized(lock2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    synchronized(lock3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        B();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        void B() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            synchronized(lock4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                synchronized(lock5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        void C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            synchronized(lock6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        void D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            synchronized(lock7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    // signal to about to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    lock7.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        Map<String, Lock[]> LOCKED_MONITORS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Lock WAITING_LOCK = lock7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int OWNED_MONITORS = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        void initExpectedResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            LOCKED_MONITORS = new HashMap<String, Lock[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            LOCKED_MONITORS.put("D", new Lock[0]); // no monitored locked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            LOCKED_MONITORS.put("C", new Lock[] {lock6});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            LOCKED_MONITORS.put("B", new Lock[] {lock5, lock4});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            LOCKED_MONITORS.put("A", new Lock[] {lock3, lock2, lock1});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            this.setExpectedResult(WAITING_LOCK, OWNED_MONITORS, LOCKED_MONITORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    static class Thread2 extends LockingThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        Map<String, Lock[]> LOCKED_MONITORS = new HashMap<String, Lock[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        Lock WAITING_LOCK = lock8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int OWNED_MONITORS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        public Thread2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            super("t2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            this.setExpectedResult(WAITING_LOCK, OWNED_MONITORS, LOCKED_MONITORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            synchronized(lock8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    synchronized(lock7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    lock8.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
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
}