Why Synchronization
The biggest problem of allowing multiple threads sharing the same data set is that one operation in one thread could collide with another operation in another threads on the same data. When this happens, the result is un-desirable.
Let's use a bank application program as an example.
Assuming that the program has multiple threads running, with each thread connecting an ATM system, and you have a saving account in the bank with $100.00, now you and your friend are going to two different ATMs at about the same time, and trying to withdraw $50.00 from your account, what do you think it will happen?
If the threads are running independently, the following could happen:
Time 01:01 02:01 03:01 04:01
+----------+---------+---------+-------
Thread 1 Get Set
Action You Account Account You
Withdraw Balance Balance Receive
$50.00 $100.00 $50.00 $50.00
Time 01:02 02:02 03:02 04:02
-+----------+---------+---------+------
Thread 2 Get Set
Action Friend Account Account Friend
Withdraw Balance Balance Receive
$50.00 $100.00 $50.00 $50.00
Time 01:01 02:01 03:01 04:01
-----------++--------++--------++------
Account $100.00 $100.00 $50.00 $50.00
Both you and your friend will receive $50.00 each, and your account will still have $50.00. The bank could lose $50.00.
The solution this problem is synchronization.
What Is Synchronization
Synchronization is a programming technique that involves 3 elements:
* Lock: An object with two states: locked and unlocked.
* Synchronized Block: A block of statements that is associated with a lock.
* Synchronization Rule: When a synchronized block is encountered in a thread of execution, the associated lock will be checked. If the lock locked, the execution will be stopped until the lock is unlocked. If the lock is unlocked, the lock will be locked, and the synchronized block of statements will be executed. When the execution reaches the end of the synchronized block, the lock will be unlocked. With this rule, two synchronized blocks associated with same lock will never be executed at the same time.
Now let's see if we can use the synchronization technique in the bank application program to help the bank. Let's define a synchronization block starting from the "Get Account Balance" action to the "Set Account Balance" action in each thread, and associate the block with a lock. With this change, both you and your friend can still withdraw $50.00, but your account will have nothing left:
Time 01:01 02:01 03:01 04:02
-----------+---------++--------++-------
Lock Unlocked Locked Locked Unlocked
Time 01:01 02:01 03:01 04:01
+----------+---------+---------+--------
Thread 1 Get Set
Action You Account Account You
Withdraw Balance Balance Receive
$50.00 $100.00 $50.00 $50.00
Time 01:02 02:02 03:02 04:02 05:02
-+----------+---------+---------+---------+------
Thread 2 Get Get Set
Action Friend Account Account Account Friend
Withdraw Balance Balance Balance Receive
$50.00 Stopped $50.00 $0.00 $50.00
Time 01:01 02:01 03:01 04:02 05:02
-----------++--------++--------++---------+------
Account $100.00 $100.00 $50.00 $0.00 $0.00
The synchronization technique did help the bank from losing money. But it also increased the total transaction time.
Synchronization Support in Java
Instead of let the programmers to design their own locks, manage the synchronization blocks, and apply the synchronization rules, Java offers a synchronization monitor on each instance of the Object class, so it can be used as a synchronization lock. Since all classes are sub classes of Object, all objects in Java can be used as synchronization locks.
Java also offers three ways to define synchronized blocks.
Synchronized Class Method:
class class_name {
static synchronized type method_name() {
statement block
}
}
All the statements in the method become the synchronized block, and the class object is the lock.
Synchronized Instance Method:
class class_name {
synchronized type method_name() {
statement block
}
}
All the statements in the method become the synchronized block, and the instance object is the lock.
Synchronized Statement:
class class_name {
type method_name() {
synchronized (object) {
statement block
}
}
}
All the statements specified in the parentheses of the synchronized statement become the synchronized block, and the object specified in the statement is the lock.
Java applys the synchronization rule by assigning the ownership of the lock's monitor to the threads that are running the synchronized blocks. Here is how it works:
* When a synchronized clock is reached in an execution thread, it will try to gain the ownership of the monitor of the lock object. If another thread owns the lock's monitor, it will wait.
* Once the lock's monitor is free, the waiting thread will become the owner of the lock's monitor, and start to execute the synchronized block.
* Once the synchronized block is executed to the end, the lock's monitor will be freed again.
Note that one program can have many locks and each lock can be associated with many different synchronized blocks. But the synchronization rule only applies between the synchronized block and its associated lock.
For example, the following code defines two synchronized blocks. Both are associated with the same lock, the instance object.
class class_name {
type method_name() {
synchronized (this) {
statement block 1
}
}
synchronized type method_name() {
statement block 2
}
}
Block 1 will never be executed at the same time as block 2.
The following code defines two synchronized blocks. But they are associated with two different locks, one is the class object, and the other is the instance object. Those two synchronized blocks will never wait for each other.
class class_name {
type method_name() {
synchronized (this) {
statement block 1
}
}
static synchronized type method_name() {
statement block 2
}
}
Wednesday, September 12, 2007
Subscribe to:
Post Comments (Atom)




0 comments:
Post a Comment