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
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.
 
wood burning stoves
 
subject: Synchronized methods
 
Similar Threads
Threads(Can someone please explain why the answer to the following questions are unpredictable)
synchronized method and thread?
Owner of an object's monitor
Exactly how syc. methods & threads work?
One question on java method call