Java Reference
In-Depth Information
As we saw earlier, when you declare methods in a class as synchronized , it prevents concurrent
execution of those methods within a single object, including concurrent execution of the same method . It is
important not to let the fact that there is only one copy of a particular method confuse you. A given
method can be potentially executing in any number of threads - as many threads as there are in the
program in fact. If it was not synchronized, the doTransaction() method could be executed
concurrently by any number of clerks.
Although this fixes the problem we had in that the account balance is now correct, the bank is still
amazingly inefficient. Each clerk is kicking their heels while another clerk is carrying out a transaction.
At any given time a maximum of one clerk is working. On this basis the bank could sack them all bar
one and get the same throughput. We can do better, as we shall see.
Synchronizing Statement Blocks
In addition to being able to synchronize methods on a class object, you can also specify a statement or a
block of code in your program as synchronized . This is more powerful, since you specify which
particular object is to benefit from the synchronization of the statement or code block, not just the
object that contains the code as in the case of a synchronized method. Here we can set a lock on any
object for a given statement block. When the block that is synchronized on the given object is
executing, no other code block or method that is synchronized on the same object can execute. To
synchronize a statement, you just write:
synchronized( theObject )
statement ; // Synchronized with respect to theObject
No other statements or statement blocks in the program that are synchronized on the object
theObject can execute while this statement is executing. Naturally, this applies even when the
statement is a call to a method, which may in turn call other methods. The statement here could equally
well be a block of code between braces. This is powerful stuff. Now we can lock a particular object
while the code block that is working is running.
To see precisely how you can use this in practice, let's create a modification of the last example. Let's up
the sophistication of our banking operation to support multiple accounts. To extend our example to
handle more than one account, we just need to make some changes to main() . We'll add one extra
account to keep the output modest, but we'll modify the code to handle any number of accounts.
Try It Out - Handling Multiple Accounts
We can modify the code in main() that creates the account and sets the initial balance to create
multiple accounts as follows:
public class BankOperation {
public static void main(String[] args) {
int[] initialBalance = {500, 800}; // The initial account balances
int[] totalCredits = new int[initialBalance.length]; //Two different cr totals
int[] totalDebits = new int[initialBalance.length]; //Two different db totals
int transactionCount = 20; // Number of debits and of credits
// Create the bank and the clerks...
Bank theBank = new Bank(); // Create a bank
Search WWH ::




Custom Search