⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blackhole.java

📁 Java多线程设计模式详解+配套源码(PDF).rar
💻 JAVA
字号:
public class Blackhole {
    public static void enter(Object obj) {
        System.out.println("Step 1");
        magic(obj);
        System.out.println("Step 2");
        synchronized (obj) {
            System.out.println("Step 3 (never reached here)");  //  不会到这里来
        }
    }
    public static void magic(final Object obj) {
        // thread会取得obj的lock,变成无穷循环的执行绪
        // 将thread的名称当作Guard条件来使用
        Thread thread = new Thread() {      // inner class
            public void run() {
                synchronized (obj) { // 在此取得obj的锁定
                    synchronized (this) {
                        this.setName("Locked"); // Guard条件的变化
                        this.notifyAll();       // 通知已经取得obj的锁定
                    }
                    while (true) {
                        // 无穷循环
                    }
                }
            }
        };
        synchronized (thread) {
            thread.setName("");
            thread.start(); // 线程的启动
            // Guarded Suspension模式
            while (thread.getName().equals("")) {
                try {
                    thread.wait(); //  等待新的线程取得obj的锁定
                } catch (InterruptedException e) {
                }
            }
        }
    }
}

⌨️ 快捷键说明

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