This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Few questions about synchronized methods and blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Few questions about synchronized methods and blocks" Watch "Few questions about synchronized methods and blocks" New topic
Author

Few questions about synchronized methods and blocks

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
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

Joined: Mar 17, 2011
Posts: 7550
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

sounds towards private lock idiom

you can google to get better source ;)
Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
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

Joined: Mar 17, 2011
Posts: 7550
    
  18

Rajiv Rai wrote:can any other thread enter any other synchronized method(method not block) for that instance.

No.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Few questions about synchronized methods and blocks
 
Similar Threads
Synchronozation How it Works
Thread
Interview question...
Synchronization and lock question
Thread