• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads-Can someone reply to this que?

 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In threads, what is the purpose of getting multiple locks on the same object by the same thread.
Eg: public void run()
{
Synchronized(this)// lock is obtained on 'this' object
{
Synchronized(this) //#2 lock is agin obtained on the 'this' obj
{
--------------
On obtaining the first lock, the thread holds the lock on the object and prevents any other thread from getting a lock on the 'this' object. so when do we go for the #2 lock.
-Sanjana
[ December 25, 2003: Message edited by: sanjana narayanan ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjana,
When I read about a thread executing more than one block of synchronized code, I think of synchronized methods being invoked in succession or recursively.

Both methods are synchronized, because m2 might be invoked without invoking m1 first.
Note: I changed the words to correct the error Vad found
[ December 25, 2003: Message edited by: Marlene Miller ]
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:
When I read about threads having multiple locks on an object, I think of a different kind of example than yours.

Multiple locks on the same object?? Hmm, doesn't any object have only one lock that becomes a subject of multithreaded contest?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Vad. You are correct, I am wrong.
An object has a lock. A thread acquires the lock of the object.
I should have said a thread performs multiple lock actions on an object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic