• 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

Static methods and thread safety

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a Utility class with all static methods. By definition, static means only one instance.I m working on a multi-threaded application where I m using these static methods to do some work for me. I m making sure that I m not declaring and class level variables and restricting variables to the methods for thread safety. I m also making sure the object references are not available outside of the method. I m hoping this would make my variables created in these static methods not accessible from outside threads.

I don't quite understand how a static method with only one instance could handle multiple threads safely. I know all method variables are placed in a stack and each thread has its own stack. But, does it mean there are multiple instances of these methods or are they accessing the only instance of the method. I would appreciate your thoughts on this topic.

Thanks,
Param
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static doesn't mean "one instance", it means "no instance". A static method is one that you can call without reference to an object (i.e., an instance of the class).

It sounds like you've got threads, static vs. instance methods, and multiple copies of code all mixed up in your head. Let me try to clear things up.

There is never more than one copy of the code for any method, whether it's static or an instance method. Multiple threads can simultaneously execute this one copy.

As you say, each thread has its own stack, and local variable in a method are allocated on that stack. That means if the method declares a local variable "x", and there are ten threads running that method simultaneously, then there are ten entirely separate "x" variables.

SO one copy of the code, and one copy of the local variables for each thread. Make sense?
 
Param Yanamandra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest for making it clear. That answers my question. I think that as long as all variables are local variables it doesn't matter whether the class is static or instance. They have their own stack trace and hence are thread safe.

Param
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method parameter is local, and if its type is mutable, it may well not be thread-safe.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Param Yanamandra:
I think that as long as all variables are local variables it doesn't matter whether the class is static or instance. They have their own stack trace and hence are thread safe.



If the method refers to some global (shared) object using one of those variables, then of course you'd have a problem with interference between multiple threads; but as long as all the data the method uses is local to that method, then yes, you're correct.
 
reply
    Bookmark Topic Watch Topic
  • New Topic