Java Reference
In-Depth Information
switch(transaction.getTransactionType()) {
case Transaction.CREDIT:
synchronized(transaction.getAccount()) {
System.out.println("Start credit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
// code to process credit...
System.out.println(" End credit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
break;
}
case Transaction.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 will produce 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 will also see from the output that
operations on different accounts do overlap. Here's a sample of what I got:
Start credit of A//C No. 2 : $800 amount: 74
End credit of A//C No. 2 : $874 amount: 74
Start debit of A//C No. 2 : $874 amount: 52
Start credit of A//C No. 1 : $500 amount: 51
End debit of A//C No. 2 : $822 amount: 52
End credit of A//C No. 1 : $551 amount: 51
Start debit of A//C No. 2 : $822 amount: 38
End debit of A//C No. 2 : $784 amount: 38
Start credit of A//C No. 2 : $784 amount: 74
End credit of A//C No. 2 : $858 amount: 74
Start debit of A//C No. 1 : $551 amount: 58
Start credit of A//C No. 2 : $858 amount: 53
End debit of A//C No. 1 : $493 amount: 58
...
Search WWH ::




Custom Search