• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronised Methods  RSS feed

 
shri mon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a thread is executing a synchronised non static method in a class is it possible for any other thread to execute any other synchronised non static method?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you are taking about the same object. Answer is No.
 
shri mon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if accross different objects
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every thread can be executing a method on a different instance of the same class. A synchronized nonstatic method is synchronized on an instance of its class.
 
shri mon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mike.

One more question.
What if the method is static?
Will the answer be still no.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lock held will be the java.lang.Class instance.
Therefore, no two static methods of a class, given a class loader, can be executed concurrently.
 
shri mon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

Can i sum it up like this?


It does not matter whether a method is static or not when it comes to synchronized keyword?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shri mon:
Thanks.

Can i sum it up like this?


It does not matter whether a method is static or not when it comes to synchronized keyword?


The rules are same for static and non -static synchronized methods but locks are differnt one is instance lock and other is class object lock.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!