Java Reference
In-Depth Information
// code to process credit...
System.out.println(" End credit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
break;
}
case DEBIT:
synchronized(transaction.getAccount()) {
System.out.println("Start debit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
// code to process debit...
System.out.println(" End debit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
break;
}
default: // We should never
get here
System.out.println("Invalid transaction");
System.exit(1);
}
}
This produces quite a lot of output, but you can always comment it out when you don't need it. You
should be able to see how a transaction for an account that is currently being worked on is always delayed
until the previous operation on the account is completed. You can also see from the output that operations
on different accounts do overlap. Here's a sample of what I got:
Start debit of A/C No. 2 : $800 amount: 34
Start credit of A/C No. 1 : $500 amount: 72
End credit of A/C No. 1 : $572 amount: 72
End debit of A/C No. 2 : $766 amount: 34
Start credit of A/C No. 2 : $766 amount: 69
End credit of A/C No. 2 : $835 amount: 69
Start debit of A/C No. 2 : $835 amount: 43
End debit of A/C No. 2 : $792 amount: 43
Start credit of A/C No. 2 : $792 amount: 59
End credit of A/C No. 2 : $851 amount: 59
Start debit of A/C No. 1 : $572 amount: 45
End debit of A/C No. 1 : $527 amount: 45
Start credit of A/C No. 1 : $527 amount: 52
End credit of A/C No. 1 : $579 amount: 52
Start debit of A/C No. 1 : $579 amount: 45
Start credit of A/C No. 2 : $851 amount: 64
...
You can see from the first two lines here that a credit for account 1 starts before the preceding debit for
account 2 is complete, so the operations are overlapped. The overlapping of operations is affected by
Search WWH ::




Custom Search