Java Reference
In-Depth Information
Using Synchronized Methods
To see how synchronization can be applied in practice, we'll construct a program that provides a simple
model of a bank. Our bank is a very young business with only one customer account initially, but we'll
have two clerks, each working flat out to process transactions for the account, one handling debits and
the other handling credits. The objects in our program are illustrated here:
Credits
theBank
clerk1
Credit operations
theAccount
Debit operations
Computer operations
are overlapped
Debits
clerk2
The bank in our model is actually a computer that performs operations on the account, and the account
is stored separately. Each clerk can communicate directly with the bank. We'll be defining four classes
that we will use in our program to model banking operations:
A Bank class to represent the bank computer.
An Account class to represent the account at the bank.
A Transaction class to represent a transaction on the account - a debit or a credit for instance.
A Clerk class to represent a bank clerk.
We will also define a class containing the method main() that will start the process off and determine
how it all works.
As we develop the code, we won't necessarily get it right first time, but we will
improve as we find out more about how to program using threads. This will expose
some of the sorts of errors and complications that can arise when you're programming
using threads.
 
Search WWH ::




Custom Search