chopstick.java

来自「初级学习资料必读本」· Java 代码 · 共 26 行

JAVA
26
字号
/* * This class requires no changes from the 1.0 version.  * It's kept here so the rest of the example can compile. */public class Chopstick {    Thread holder = null;    public synchronized void grab() throws InterruptedException {        while (holder != null)            wait();        holder = Thread.currentThread();    }    public synchronized void release() {        holder = null;        notify();    }    public synchronized void releaseIfMine() {        if (holder == Thread.currentThread())            holder = null;        notify();    }}

⌨️ 快捷键说明

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