File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rahul Kumar Tiwari
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nitesh Kant for that information, BTW, what does mean by OP?
Nitesh Kant wrote:
Although the OP did not.....
 
Paul Clapham
Sheriff
Pie
Posts: 20163
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Thanks Nitesh Kant for that information, BTW, what does mean by OP?


OP = Original Poster
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
OP = Original Poster


OK, Thanks...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic