• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

// object lock doubt

 
Meg Adal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Exam Cram book they have stated like this:

A single thread can obtain locks on many objects and/or multiple locks on same object

I think last part of statement is incorrect.
I guess as there is only one lock with given object ,thread can't obtain multiple locks on same object
correct me if I am wrong

Thanks in advance
Megan
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine that the methods are protected by different locks as it follows:
method1() { synchronized(lock1) {method2();} }
method2() { synchronized(lock2) {} }
A thread calling method1 will obtain two locks (if available)
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, see this link.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But those are two locks on two different objects (lock1 and lock2).
I think the statement really means that the same thread can lock the same object more than once, so this code won't deadlock:
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I think it's more like re-acquiring the lock of an object than obtaining multiple locks of the same object. This is to prevent instances like a thread waiting for a lock that it already has.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic