jdk/src/share/demo/management/FullThreadDump/Deadlock.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
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 2004-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.CyclicBarrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.BrokenBarrierException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.locks.Lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.locks.ReentrantLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This Deadlock class demonstrates the capability of performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * deadlock detection programmatically within the application using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the java.lang.management API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * See ThreadMonitor.java for the use of java.lang.management.ThreadMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class Deadlock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void main(String[] argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Deadlock dl = new Deadlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // Now find deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        ThreadMonitor monitor = new ThreadMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        while (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            found = monitor.findDeadlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                Thread.sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println("\nPress <Enter> to exit this Deadlock program.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        waitForEnterPressed();
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
    private CyclicBarrier barrier = new CyclicBarrier(6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public Deadlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        DeadlockThread[] dThreads = new DeadlockThread[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Monitor a = new Monitor("a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Monitor b = new Monitor("b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Monitor c = new Monitor("c");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        dThreads[0] = new DeadlockThread("MThread-1", a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        dThreads[1] = new DeadlockThread("MThread-2", b, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        dThreads[2] = new DeadlockThread("MThread-3", c, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Lock d = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Lock e = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Lock f = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        dThreads[3] = new DeadlockThread("SThread-4", d, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        dThreads[4] = new DeadlockThread("SThread-5", e, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        dThreads[5] = new DeadlockThread("SThread-6", f, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // make them daemon threads so that the test will exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (int i = 0; i < 6; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            dThreads[i].setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            dThreads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    class DeadlockThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        private Lock lock1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        private Lock lock2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        private Monitor mon1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        private Monitor mon2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        private boolean useSync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        DeadlockThread(String name, Lock lock1, Lock lock2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            this.lock1 = lock1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            this.lock2 = lock2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            this.useSync = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        DeadlockThread(String name, Monitor mon1, Monitor mon2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            this.mon1 = mon1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            this.mon2 = mon2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            this.useSync = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (useSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                syncLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                monitorLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        private void syncLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            lock1.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                } catch (BrokenBarrierException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                goSyncDeadlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                lock1.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        private void goSyncDeadlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } catch (BrokenBarrierException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            lock2.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new RuntimeException("should not reach here.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private void monitorLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            synchronized (mon1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                } catch (BrokenBarrierException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                goMonitorDeadlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        private void goMonitorDeadlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } catch (BrokenBarrierException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            synchronized (mon2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throw new RuntimeException(getName() + " should not reach here.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    class Monitor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Monitor(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static void waitForEnterPressed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                char ch = (char) System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (ch<0||ch=='\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}