jdk/test/java/lang/management/ThreadMXBean/LockingThread.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2933 08ea3ecb912c
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 2005 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
 * @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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static long[] getThreadIds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return new long[] {t1.getId(), t2.getId()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static void checkLockedMonitors(ThreadInfo[] tinfos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int matches = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for (ThreadInfo ti : tinfos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (ti.getThreadId() == t1.getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                t1.checkLockedMonitors(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (ti.getThreadId() == t2.getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                t2.checkLockedMonitors(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (matches != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new RuntimeException("MonitorInfo missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static class Lock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Lock(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    final String threadName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    Lock         waitingLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    int          numOwnedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    Map<String, Lock[]> ownedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public LockingThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.threadName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected void setExpectedResult(Lock waitingLock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                     int numOwnedMonitors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                     Map<String, Lock[]> ownedMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.waitingLock = waitingLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.numOwnedMonitors = numOwnedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.ownedMonitors = ownedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void checkLockedMonitors(ThreadInfo info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        checkThreadInfo(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        MonitorInfo[] monitors = info.getLockedMonitors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (monitors.length != numOwnedMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            ThreadDump.threadDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new RuntimeException("Number of locked monitors = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                monitors.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                " not matched. Expected: " + numOwnedMonitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // check if each monitor returned in the list is the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (MonitorInfo m : monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            StackTraceElement ste = m.getLockedStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int depth = m.getLockedStackDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            checkStackFrame(info, ste, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            checkMonitor(m, ste.getMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // check if each expected monitor is included in the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (Map.Entry<String, Lock[]> e : ownedMonitors.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            for (Lock l : e.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                checkMonitor(e.getKey(), l, monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (info.getLockedSynchronizers().length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            ThreadDump.threadDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new RuntimeException("Number of locked synchronizers = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                info.getLockedSynchronizers().length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                " not matched. Expected: 0.");
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
    void checkThreadInfo(ThreadInfo info) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (!getName().equals(info.getThreadName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new RuntimeException("Name: " + info.getThreadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                " not matched. Expected: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        LockInfo l = info.getLockInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if ((waitingLock == null && l != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            (waitingLock != null && l == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new RuntimeException("LockInfo: " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                " not matched. Expected: " + waitingLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        String waitingLockName = waitingLock.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int hcode = System.identityHashCode(waitingLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (!waitingLockName.equals(l.getClassName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new RuntimeException("LockInfo : " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                " class name not matched. Expected: " + waitingLockName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (hcode != l.getIdentityHashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new RuntimeException("LockInfo: " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                " IdentityHashCode not matched. Expected: " + hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        String lockName = info.getLockName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        String[] s = lockName.split("@");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (!waitingLockName.equals(s[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new RuntimeException("LockName: " + lockName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                " class name not matched. Expected: " + waitingLockName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int i = Integer.parseInt(s[1], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (hcode != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new RuntimeException("LockName: " + lockName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                " IdentityHashCode not matched. Expected: " + hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    void checkStackFrame(ThreadInfo info, StackTraceElement ste, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        StackTraceElement[] stacktrace = info.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (!ste.equals(stacktrace[depth])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            System.out.println("LockedStackFrame:- " + ste);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            System.out.println("StackTrace at " + depth + " :-" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                stacktrace[depth]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new RuntimeException("LockedStackFrame does not match " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                "stack frame in ThreadInfo.getStackTrace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    void checkMonitor(MonitorInfo m, String methodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (Map.Entry<String, Lock[]> e : ownedMonitors.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (methodName.equals(e.getKey())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                for (Lock l : e.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    String className = l.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    int hcode = System.identityHashCode(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    if (className.equals(m.getClassName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        hcode == m.getIdentityHashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        // monitor matched the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throw new RuntimeException("Monitor not expected" + m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    void checkMonitor(String methodName, Lock l, MonitorInfo[] monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        String className = l.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int hcode = System.identityHashCode(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        for (MonitorInfo m : monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (className.equals(m.getClassName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                hcode == m.getIdentityHashCode() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                methodName.equals(m.getLockedStackFrame().getMethodName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        throw new RuntimeException("Monitor not found in the returned list" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            " Method: " + methodName + " Lock: " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    static class Thread1 extends LockingThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        public Thread1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            super("t1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            initExpectedResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            synchronized(lock1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                synchronized(lock2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    synchronized(lock3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        B();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        void B() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            synchronized(lock4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                synchronized(lock5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        void C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            synchronized(lock6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        void D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            synchronized(lock7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    // signal to about to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    lock7.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        Map<String, Lock[]> LOCKED_MONITORS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        Lock WAITING_LOCK = lock7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int OWNED_MONITORS = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        void initExpectedResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            LOCKED_MONITORS = new HashMap<String, Lock[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            LOCKED_MONITORS.put("D", new Lock[0]); // no monitored locked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            LOCKED_MONITORS.put("C", new Lock[] {lock6});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            LOCKED_MONITORS.put("B", new Lock[] {lock5, lock4});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            LOCKED_MONITORS.put("A", new Lock[] {lock3, lock2, lock1});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            this.setExpectedResult(WAITING_LOCK, OWNED_MONITORS, LOCKED_MONITORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    static class Thread2 extends LockingThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        Map<String, Lock[]> LOCKED_MONITORS = new HashMap<String, Lock[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        Lock WAITING_LOCK = lock8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int OWNED_MONITORS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        public Thread2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            super("t2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            this.setExpectedResult(WAITING_LOCK, OWNED_MONITORS, LOCKED_MONITORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            synchronized(lock8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    synchronized(lock7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    lock8.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
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
}