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

📄 cubbyhole.java

📁 java实现的线程同步
💻 JAVA
字号:
public class CubbyHole {
    private int contents;
    private int available = 1;

    public synchronized int get() {
        while (available != 0) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        available = 1;
        notifyAll();
        return contents;
    }

    public synchronized void put(int value) {
        while (available !=1) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        contents = value;
        available = 2;
        notifyAll();
    }


  public synchronized void add() {
          while (available !=2) {
              try {
                  wait();
              } catch (InterruptedException e) { }
          }
          contents=contents+10;
          available = 0;
          notifyAll();
      }
  }

⌨️ 快捷键说明

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