aspose file tools
The moose likes Threads and Synchronization and the fly likes Are static variable and method threadsafe? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Are static variable and method threadsafe?" Watch "Are static variable and method threadsafe?" New topic
Author

Are static variable and method threadsafe?

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
Are static variable and method threadsafe? if so Could you explain with some samples?
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 488
Hi,
Static means shared across all the instances of that Class in a JVM. Shared resources are not thread-safe.Hence Static method or variables are not thread safe.
If you are talking about declaring constants using static and final keyword then because they are read-only, they are thread safe.

You have to synchronize the static method if you want thread safe nature. This requires the Thread that want to execute the static method to obtain Class level lock.


Regards,
amit
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3053
    
    1

Thread safety is completely unrelated to specifiers like static. Thread safety depends on a lot of factors. You can easily have a static field that is thread safe by virtue of it being final and immutable. Or if your methods only ever read from it, you don't have to worry about concurrent access either.

Are static variables and methods inherently thread safe? No.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 370

Thread safety is completely unrelated to specifiers like static


hmm a bit strong, mostly unrelated, static initialisation comes with stronger guarantees under the new memory model and I've seen that used quite a bit. So being static could help in that instance , though not make it thread safe on its own i.e. with initialisation you would have to consider its static ness in determining thread safety.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Are static variable and method threadsafe?
 
Similar Threads
Is static calls within Servlet performant
which data is ThreadSafe ?
Are static variable and method threadsafe? if so why?
Are implicit objects in JSP are thread safe???