vmdeadlock.java

来自「JGRoups源码」· Java 代码 · 共 62 行

JAVA
62 行
字号
package org.jgroups.tests;

import org.jgroups.util.Util;

/**
 * @author Bela Ban
 * @version $Id$
 */
public class VMDeadlock {
    static final Object mutex_one=new Object();
    static final Object mutex_two=new Object();

    public static void main(String[] args) throws InterruptedException {
        new VMDeadlock().start();
    }

    private void start() throws InterruptedException {
        MyThreadOne t1=new MyThreadOne();
        MyThreaTwo t2=new MyThreaTwo();

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }


    static class MyThreadOne extends Thread {
        public void run() {

            synchronized(mutex_one) {
                Util.sleep(2000);


                synchronized(mutex_two) {
                    // we won't get here
                    System.out.println("acquired 2");
                }
            }

        }
    }

    static class MyThreaTwo extends Thread {
        public void run() {

            synchronized(mutex_two) {
                Util.sleep(2000);


                synchronized(mutex_one) {
                    // we won't get here
                    System.out.println("acquired 1");
                }
            }

        }
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?