selectable.java

来自「演示win32的socket 通讯 八皇后的改进算法 并发Concurren」· Java 代码 · 共 58 行

JAVA
58
字号
package concurrency.message;


class Selectable {
    private boolean open = false;
    private int ready = 0;
    private Select inchoice = null;
    private boolean guard_ = true;

    void setSelect(Select s) {
        inchoice = s;
    }

    synchronized void block() throws InterruptedException {
          if (inchoice == null) {
            setOpen();
            while(ready==0) wait();
            clearOpen();
        }
    }

    synchronized void signal() {
        if (inchoice == null) {
            ++ready;
            if (open) notifyAll();
        } else {
            synchronized (inchoice) {
                ++ready;
                if (open) inchoice.notifyAll();
            }
        }
    }

    boolean testReady() {
        return ready>0;
    }

    synchronized void clearReady() {
        --ready;
    }

    void setOpen() {
        open=true;
    }

    void clearOpen() {
         open=false;
    }

    public void guard(boolean g) {
        guard_=g;
    }

    boolean testGuard(){
        return guard_;
    }
}

⌨️ 快捷键说明

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