Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread synchronized method

 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks gurpeet, I got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic