Java Reference
In-Depth Information
TRY IT OUT: Defining a Bank Clerk
A clerk is a slightly more complicated animal. He or she retains information about the bank and details
of the current transaction, and is responsible for initiating debits and credits on an account by commu-
nication with the central bank. Each clerk works independently of the others so they are each a separate
thread:
public class Clerk implements Runnable {
// 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) {
// Non-stop work...
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!
}
private Bank theBank;
// The employer - an electronic
marvel
private Transaction inTray;
// The in-tray holding a
transaction
}
Search WWH ::




Custom Search