Notes

Powered by The LE Company

Wednesday, November 10, 2010

"synchronized" - How Java Supports Synchronization

(from: http://www.herongyang.com/Java/Synchronization-Support-in-Java-synchronized.html)

This section describes the 'synchronized' keyword and how Java supports synchronization in 3 different ways - synchronized class methods, synchronized instance methods, synchronized statement blocks.

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
}
}

No comments: