• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Few questions about synchronized methods and blocks

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading the SCJP 5 book chapter on Threads. To quote from
the synchronization objective

"When you synchronize a method, the object used to invoke the method is the
object whose lock must be acquired. But 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 the above mean? I can't understand the part about the synhronized block
in the above paragraph

Also, is the following true

If there is a class which has a synchronized method and a synchronized block,
if a thread enters a synchronized block, no other thread can execute the synchronized
method on the same instance. Just like if a thread enters a synchronized method , no other thread
can execute any synchronized methods on the same instance


Am stuck on this part,
please help to understand

Thanks




 
Winston Gutkowski
Bartender
Pie
Posts: 10268
60
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:"When you synchronize a method, the object used to invoke the method is the
object whose lock must be acquired. But 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."

At the risk of verging on the smutty: have you ever used the toilet at a small gas station? Usually, you have to get a key from the cashier, which you return when you're done. This simple procedure prevents you from walking in on someone else while they're doing their business.

Now imagine that the code you want to execute is inside such a restroom. Anyone who wants to execute it has to get the key before they can execute it, and if two people need to use it at the same time, the second person has to wait until the first one is finished.

A slightly larger gas station might have separate toilets for men and women. That means that two people can use the toilets at the same time providing they're of different sexes. A larger one still might have stalls to accommodate more people, but still, only one person at a time will be able to use a particular stall.

It's exactly the same with synchronization: When you synchronize a method you're telling Java that any Thread that wants to execute it has to "get the key" before it can proceed, and all other Threads have to wait unless the first one has returned the key to the cashier.

The only difference between a synchronized method and a synchronized block is that with the latter you tell Java which key you want to use.

Winston
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sounds towards private lock idiom

you can google to get better source ;)
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies but I still have not got the answer to my second question

When we use synchronized block with "this" , i mean current object,
can any other thread enter any other synchronized method(method not block) for that instance.
 
Winston Gutkowski
Bartender
Pie
Posts: 10268
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:can any other thread enter any other synchronized method(method not block) for that instance.

No.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic