• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 Synchronized methods

 
Shailendra Bade
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we have 2 synchronized methods in a class. One thread is accesing first synchronized method. Will another thread be allowed to access the second synchronized method? How will the monitor state/locks will be managed?
------------------
Shailendra Bade
Mumbai, India
Mail to: bades@vsnl.com
 
David Harrigan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
Once a thread enters a synchronized block (method or whatever) then that thread "owns" that entire object until the lock is released. No other thread will be able to access the syncrhonized methods in that object, unless the synchronized block calls a wait().
David.
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Just to clarify, your response is correct in the case of two <code>synchronized</code> methods but not necessarily in the cases of one sync'ed method and one sync'ed statement, or two sync'ed statements. If the two critical sections are sync'ed on different monitor objects, then they are not mutually exclusive.<pre>
class A {
public synchronized void A() {
...
}
public void B() {
synchronized (someOtherObject) {
...
}
}
}</pre>
Separate threads may execute simultaneously in A() and B().
Jerry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!