Java Reference
In-Depth Information
Thread.sleep(25); // Busy so try
later
} catch(InterruptedException e) {
System.out.println(e);
}
}
clerk1.doTransaction(transaction); // Now do the
credit
amount = 30 + rand.nextInt(31); // Generate
amount of $30 to $60
transaction = new Transaction(account, // Account
TransactionType.DEBIT, // Debit
transaction
amount); // of amount
totalDebits += amount; // Keep total
debit tally
// Wait until the second clerk is free
while(clerk2.isBusy()) {
try {
Thread.sleep(25); // Busy so try
later
} catch(InterruptedException e) {
System.out.println(e);
}
}
clerk2.doTransaction(transaction); // Now do the
debit
}
After all the transactions have been processed, you can output the results. However, the clerks could still
be busy after you exit from the loop, so you need to wait for both of them to be free before outputting the
results. You can do this with a while loop:
// Wait until both clerks are done
while(clerk1.isBusy() || clerk2.isBusy()) {
try {
Thread.sleep(25);
} catch(InterruptedException e) {
System.out.println(e);
}
}
Lastly, you output the results:
// Now output the results
System.out.println(
"Original balance : $" + initialBalance+"\n" +
"Total credits : $" + totalCredits+"\n" +
Search WWH ::




Custom Search