aspose file tools
The moose likes Threads and Synchronization and the fly likes 2 Synchronized methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "2 Synchronized methods" Watch "2 Synchronized methods" New topic
Author

2 Synchronized methods

Shailendra Bade
Greenhorn

Joined: Jul 04, 2000
Posts: 22
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


Shailendra Bade<BR>Mumbai, India<BR>Mail to: bades@vsnl.com
David Harrigan
Ranch Hand

Joined: Dec 12, 2000
Posts: 52
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

Joined: Sep 19, 2000
Posts: 221
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: 2 Synchronized methods
 
Similar Threads
threads
Threads
Question about wait() method
threads
synchronized block/method