This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrent execution  RSS feed

 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers


I have a class that contains 2 synchronised methods.I have 2 threads .Can these 2 threads concurrently execute these 2 methods(ie. First thread on First synchronised method and Second thread on Second synchronised method at the same time)If not why??
 
Henry Wong
author
Sheriff
Posts: 23260
124
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 sudhana madhu:
I have a class that contains 2 synchronised methods.I have 2 threads .Can these 2 threads concurrently execute these 2 methods(ie. First thread on First synchronised method and Second thread on Second synchronised method at the same time)If not why??


Whether two threads can run concurrently depends on the locks that they use to synchronize with. Static methods use the Class instance that represents the class. Non-static methods use the 'this' object.

So... If one method is static and the other non-static, they can run concurrently. Also, if neither method is static, and they call different instances, they can run concurrently.

If both methods are static, or if both methods are not static (with the same instance), then no, they can *not* run concurrently.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!