wood burning stoves 2.0*
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


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: 924
    
    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.
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: 924
    
    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.
 
GeeCON Prague 2014
 
subject: Thread synchronized method