my dog learned polymorphism
The moose likes Threads and Synchronization and the fly likes Grabbing the same lock more than once Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Grabbing the same lock more than once" Watch "Grabbing the same lock more than once" New topic
Author

Grabbing the same lock more than once

Victor Ho
Ranch Hand

Joined: Sep 05, 2003
Posts: 74
I would like to find out if there is an extra sigificant overhead of grabbing the same lock more than once.
e.g. void fooA() {
synchronized (m_lock) { ... }
}

void fooB() {
synchronized (m_lock) { fooA(); }
}
When I call fooB(), the flow would grab m_lock twice.
I feel this is a design tradeoff. I could have removed the synchronized() in fooA if I am 100% sure fooB is the only place calling it, but to be clean, fooA() should not rely on anything outside its scope if it needs some serialization inside its own logic.
Any advise or common practices?
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
If fooA is private, I'd probably remove the sync from within it. But either way, I'd say there's no "significant" overhead or performance penalty for locking it twice. In reality, you'll only ending up with one lock, not two anyway.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Grabbing the same lock more than once
 
Similar Threads
Lock and Unlock Design Question
Why it's Zero?
Why can't interfaces be final?
Question about synchronized
How does the lock work in this case?