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

📄 museum.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.io.*;

class Museum {

  Museum (int w) {
    walkmen = w;
    cash = 0;
  }

  synchronized void hire (int c,int n) {
    // If there are not enough Walkmen left,
    // wait until someone at another counter returns
    // some and notifies us accordingly.
    // If the returns are not enough, we'll carry on
    // waiting.
    System.out.println("Counter "+c+" wants "+n);
    while (walkmen < n) {
      try { wait(); }
      catch (InterruptedException e) {}
    }

    // Hire out the Walkmen and take the deposit.
    // Let the customers at this counter "walk away"
    // by relinquishing control of the monitor with
    // a notify call.
    walkmen -= n;
    cash += n;
    System.out.println("Counter "+c+" acquires "+n);
    System.out.println("Pool status:"+
      " Deposits "+cash+" Total "+(walkmen+cash)
      + " Walkmen "+walkmen);
    notify ();
  }

  synchronized void replace (int n) {

    // Always accept replacements immediately.
    // Once the pool and deposits have been updated,
    // notify any other helper waiting for Walkmen.
    System.out.println("Replacing "+n);
    walkmen +=n;
    cash -= n;
    notify ();
  }

  private static int walkmen;
  private static int cash;
}

⌨️ 快捷键说明

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