File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized block

 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
rohan yadav
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that I know, what I am not getting is lock on third party object thing in the line
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Useful information can be found Here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic