• 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

Question on static synchronized methods

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking for some clarification on static synchronized methods.

Say for example, I have a target runnable named tr created by implementing Runnable. This class name is MyRunner. MyRunner has (besides an overridden run method) two STATIC synchronized methods, methodA and methodB.

In the main of MyTestClass, I have instantiated two thread objects, named a and b, passing tr as the target. I then start thread a & b, one after another.

Am I to understand there is only 1 lock for Class MyRunner?

And if this is true, then would this imply either method methodA or methodB can ever be running at any one time?

Thanks,
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code instead of explaining it. It is hard to visualize the code from the description.

static synchronized methods will lock on the class object of the enclosing type.
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...Am I to understand there is only 1 lock for Class MyRunner? ....



Every object in java has a monitor(lock and monitors are fungible in Java, I guess). For static synchronized methods, the lock used is of the "EnclosingClassName.class" Class Object. There would be one java.lang.Classobject per type loaded[comments?].So, essentially, the two static synchronized methods would be using the same lock, and therefore they wont run in parallel.

I second Mr. Deepak; please post some code. If you post something to see rather than visualize you get better (more focused) and faster replies.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic