• Post Reply Bookmark Topic Watch Topic
  • New Topic

Grabbing the same lock more than once

 
Victor Ho
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!