• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronized methods

 
Shrawan Bhageria
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please clear this.

If 2 different threads hit 2 different synchronized methods in an object at the same time will they both continue?

Thanks in advance.

Regards,

Shrawan
 
Henry Wong
author
Sheriff
Posts: 22516
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shrawan Bhageria:
Hi all,

Please clear this.

If 2 different threads hit 2 different synchronized methods in an object at the same time will they both continue?

Thanks in advance.

Regards,
Shrawan


Synchronization is based on Objects -- they acquire locks specific to the object.

If neither method is static and they both call methods on the same instance, then one will block until the other completes. If both are static and the objects are of the same class type, then one will block until the other completes.

If neither method is static and they call methods on different instances, then they both continue. If one calls a static method and the other calls a non-static method, then they both continue.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two synchronized instance methods called on the same object? No, one of the threads will have to wait for the other to return from its method. Synchronized methods are not independent of one another.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!