It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread synchronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread synchronized method" Watch "Thread synchronized method" New topic
Author

Thread synchronized method

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

K&B book , chapter 9 thread --> page 736,

It stats :
A thread can acquire more than one lock. For example, a thread can enter a
synchronized method, thus acquiring a lock, and then immediately invoke
a synchronized method on a different object
, thus acquiring that lock as
well. As the stack unwinds, locks are released again. Also, if a thread acquires
a lock and then attempts to call a synchronized method on that same
object, no problem.


I can't get how the thread can acquire lock on a different object also that already acquire a lock on one object.

Thanks in advance,
-Ankit.


OCPJP 6 (91%)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

Ankit Gareta wrote: K&B book , chapter 9 thread --> page 736,

It stats :
A thread can acquire more than one lock. For example, a thread can enter a
synchronized method, thus acquiring a lock, and then immediately invoke
a synchronized method on a different object
, thus acquiring that lock as
well. As the stack unwinds, locks are released again. Also, if a thread acquires
a lock and then attempts to call a synchronized method on that same
object, no problem.


I can't get how the thread can acquire lock on a different object also that already acquire a lock on one object.

Thanks in advance,
-Ankit.



Every object has built in lock which is acquired by a thread entering that objects synchronized section. as said a thread can acquire lock on more than one object. there is no compulsion that thread should acquire only one monitor/lock.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks for your reply gurpeet,

But i don't get it. Can you please provide the example of "a thread can enter a synchronized method, thus acquiring a lock, and then immediately invoke
a synchronized method on a different object " ?

-Ankit.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

Ankit Gareta wrote:Thanks for your reply gurpeet,

But i don't get it. Can you please provide the example of "a thread can enter a synchronized method, thus acquiring a lock, and then immediately invoke
a synchronized method on a different object " ?

-Ankit.




Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks gurpeet, I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread synchronized method
 
Similar Threads
Entangled in Threads
static synchronized
how thread can aquire more than one lock
Please explain
Threads: Doubt