Java Reference
In-Depth Information
Bank theBank = new Bank();
// Create
a bank
Vector<Clerk> clerks = new Vector<Clerk>();
// Stores
the clerk
Vector<Account> accounts = new Vector<Account>();
// Stores
the accounts
for(int i = 0 ; i < clerkCount ; ++i) {
clerks.add(new Clerk(i+1, theBank));
// Create
the clerks
}
for(int i = 0 ; i < initialBalance.length ; ++i) {
accounts.add(new Account(i+1, initialBalance[i])); // Create
accounts
totalCredits[i] = totalDebits[i] = 0;
}
ExecutorService threadPool = Executors.newCachedThreadPool();
// Create and start the transaction source threads
Future<int[]> credits = threadPool.submit(new TransactionSource(
TransactionType.CREDIT, transactionCount, accounts,
clerks));
Future<int[]> debits = threadPool.submit(new TransactionSource(
TransactionType.DEBIT, transactionCount, accounts,
clerks));
// Create and start the clerk threads
for(Clerk clerk : clerks) {
threadPool.submit(clerk);
}
try {
totalCredits = credits.get();
totalDebits = debits.get();
} catch(ExecutionException e) {
System.out.println(e.getCause());
} catch(InterruptedException e) {
System.out.println(e);
}
// Orderly shutdown when all threads have ended
threadPool.shutdown();
try {
threadPool.awaitTermination(10L, TimeUnit.SECONDS);
} catch(InterruptedException e) {
System.out.println(e);
}
if(threadPool.isTerminated()) {
System.out.println("\nAll clerks have completed their
tasks.\n");
} else {
Search WWH ::




Custom Search