• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Concurrent execution

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic