| Author |
synchronized block
|
Neha Daga
Ranch Hand
Joined: Oct 30, 2009
Posts: 504
|
|
when you synchronize a block of code, you specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object.
what does this mean?
|
SCJP 1.6 96%
|
 |
rohan yadav
Ranch Hand
Joined: Oct 13, 2009
Posts: 156
|
|
When you enter in Synchronized block, you must have lock of object you have specified in Synchronized block
e.g
In above example you must have lock on object from which you are invoking doStuff() method.
|
Sage of The Monstrous Toad of Mount Myoboku
|
 |
Neha Daga
Ranch Hand
Joined: Oct 30, 2009
Posts: 504
|
|
|
that I know, what I am not getting is lock on third party object thing in the line
|
 |
Chinna Eranna
Ranch Hand
Joined: Dec 08, 2009
Posts: 174
|
|
"lock on third party"
When you synchronize on the method, then by default, the synchronization happens on the object of that class.
But by using the block synchronization, you can lock using objects of other class.
|
- Chinna
|
 |
Neha Daga
Ranch Hand
Joined: Oct 30, 2009
Posts: 504
|
|
thats what i want to know why should we lock on objects of other class when accessing members of this class.
|
 |
Chinna Eranna
Ranch Hand
Joined: Dec 08, 2009
Posts: 174
|
|
Simple example would.. be if a block has to be synchronized for all the objects of current class .. then
we have to some thing like...
synchronized(ThisClassName.class){
}
|
 |
Hosam Shahin
Greenhorn
Joined: Dec 21, 2009
Posts: 7
|
|
|
Useful information can be found Here
|
IBM Certified System Admin. - WebSphere MQ V6.0 / Message Broker V6
SCJP6 is in progress.....
Book: SCJP Sun Certified Programmer for Java 6 Exam 310-065 ...
Chapter 4: studying the book for 2nd time
|
 |
 |
|
|
subject: synchronized block
|
|
|