This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized block" Watch "synchronized block" New topic
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
 
Similar Threads
Doubt on Thread Lock
Thread
About synchoronized code blocks in java
Difference between synchronized method and synchrozed block
Thread