Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Threads and Synchronization and the fly likes Two Threading access the different Synchronize method in a same object. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Two Threading access the different Synchronize method in a same object." Watch "Two Threading access the different Synchronize method in a same object." New topic
Author

Two Threading access the different Synchronize method in a same object.

Rahul Kumar Tiwari
Greenhorn

Joined: Jun 09, 2008
Posts: 14
Hi Everyone

The scenario is that, There is a class A which has two synchronize method like aa() and bb(), and many other un-synchronize method. Can it is possible that two different thread like YY, and ZZ can access the method aa() and bb() in the same object of class in same time.

If yes then please explain. because i have read that there is a one lock per object. then how can thread YY access aa() method and thread ZZ access bb() method same time.

Thank
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Two different Threads can't access a synchronized methods on a single object! But One Thread can access different synchronized methods on a single object!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Rahul Kumar Tiwari wrote:The scenario is that, There is a class A which has two synchronize method like aa() and bb(), and many other un-synchronize method. Can it is possible that two different thread like YY, and ZZ can access the method aa() and bb() in the same object of class in same time.


No it is not possible and your reasoning, as to why it is not possible, is correct.

Abimaran wrote: But One Thread can access different synchronized methods on a single object!


Although the OP did not ask this scenario but this is a good information.
The reason is that all locks in java are re-entrant. So, if the current thread holds the lock then it will not contend again for the same lock. To make it more explicit, the concurrent utility libraries names the lock classes as ReentrantLock.


apigee, a better way to API!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Nitesh Kant for that information, BTW, what does mean by OP?
Nitesh Kant wrote:
Although the OP did not.....
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Abimaran Kugathasan wrote:Thanks Nitesh Kant for that information, BTW, what does mean by OP?


OP = Original Poster
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Paul Clapham wrote:
OP = Original Poster


OK, Thanks...
 
jQuery in Action, 2nd edition
 
subject: Two Threading access the different Synchronize method in a same object.
 
Similar Threads
Synchronized
Member object talks back to parent object
Sorting question
synchronized threads
intermediate submit and validation of a form