wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronized methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronized methods" Watch "Synchronized methods" New topic
Author

Synchronized methods

geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
Hi,
Lets say a class has two synchronized methods A() and B() and there are two threads TA and TB. Is it possible for TA to execute A() and TB to execute method B()independently at the same time? In other words, when a thread acquires the monitor of an object to execute a synchronized method, does it have the monitor for the specific method of the object or for the whole of the object?
Thanks.
srini v
Greenhorn

Joined: Dec 15, 2003
Posts: 11
Hi:

does it have the monitor for the specific method of the object or for the whole of the object?

The lock is for the whole of the object.
Also note that java threads are reentrant.
cheers
angelo celeste
Ranch Hand

Joined: Mar 21, 2001
Posts: 73
Hi Geeta.
Have a look at this.
It may help.
http://www.csc.vill.edu/~mdamian/threads/javamonitors.html
-Angelo
srini v
Greenhorn

Joined: Dec 15, 2003
Posts: 11
oops! Java locks (not threads) are reentrant.
geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
Thanks Angelo, Srinivasan for your help.
 
Don't get me started about those stupid light bulbs.
 
subject: Synchronized methods