• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads-Can someone reply to this que?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic