This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads-Can someone reply to this que? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads-Can someone reply to this que?" Watch "Threads-Can someone reply to this que?" New topic
Author

Threads-Can someone reply to this que?

sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: Mar 05, 2003
Posts: 1391
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads-Can someone reply to this que?
 
Similar Threads
Whose lock will be obtained
need synchronize clarification
synchrinized code doubt
threads synchronized methods
Does each instance of the Object own a lock OR .....