• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

static method

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I understand about static method is: we don't have to instantiate an object of a class (where the static method is) to call the static method.
But I have a lot of questions regarding it.
how are static methods handled in a multithreaded application?
I have a class ABC with a static method public static stat(ArrayList a). now assume there are more threads calling at the same time ABC.stat(a). What happens? have some threads to wait until the others have finished calling the method, or can static methods be multithreaded, (can they perform simultaniously)?
What really happens behind the sceen? I mean what happens in the memory level and so on.
If there is a good link where I can read some more things about static methods please post it as a reply, I would be more than happy to read it
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shubh, please do not cross-post, -- we consider it a criminal offense here at JavaRanch.
The synchronized static methods are handled the same as the synchronized instance methods, conceptually. However, in case of the synchronized instance method, the this object serves as a lock, while in the case of synchronized static method, the java.lang.Class object that refers to the class serves as a lock.
Since the synchronized instance and static methods synchronize on different objects, they can run simultaneusly (unlike the synchronized instance methods). For example:

Output:
in instanceMethod
in classMethod
leaving instanceMethod
leaving classMethod
So, can a static synchronized method run at the same time with the instance synchronized method? -- Yes, because they lock on different objects.
Can two static synchronized methods or two instance synchronized methods run at the same time? -- No, because they lock on the same object.
Does it mean that a static synchronized method and an instance synchronized method may corrupt the object state when running simulteneusly? -- Absolutely, so think it through.
[ May 07, 2003: Message edited by: Eugene Kononov ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic