clerk.java

来自「非常好的java事例以及带源码事例的java2教程」· Java 代码 · 共 45 行

JAVA
45
字号
public class Clerk implements Runnable
{
  private Bank theBank;               // The employer - an electronic marvel
  private Transaction inTray;         // The in-tray holding a transaction

  // Constructor
  public Clerk(Bank theBank)
  {
    this.theBank = theBank;           // Who the clerk works for
    inTray = null;                    // No transaction initially
  }

  // Receive a transaction
  public void doTransaction(Transaction transaction)
  {  inTray = transaction;  }

  // The working clerk...
  public void run()
  {
    while(true)
    {
      while(inTray == null)           // No transaction waiting?
      {
        try
        {
          Thread.sleep(150);          // Then take a break...
        }
        catch(InterruptedException e)
        {
          System.out.println(e);
        }
      }
 
      theBank.doTransaction(inTray);
      inTray = null;                  // In-tray is empty
    }
  }

  // Busy check
  public boolean isBusy()
  {
    return inTray != null;            // A full in-tray means busy!
  }
}

⌨️ 快捷键说明

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