aspose file tools
The moose likes Threads and Synchronization and the fly likes static methods and threads 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 "static methods and threads" Watch "static methods and threads" New topic
Author

static methods and threads

Jeremy Hare
Greenhorn

Joined: May 23, 2001
Posts: 7
Quick question. Are static methods thread safe? ie. can 2 threads run the same static method simultaneously?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
There's nothing special about static methods, with regard to thread safety. If they access shared objects, then such access will usually need to be controlled by synchronisation; this might be on the object being accessed, some higher-level object or a special object allocated purely as a lock. The only way that you might think of them as special is that there's no instance of the Class on which you can synchronise.
You can declare a static method as synchronised. If you do, it will be synchronised on the single Class object of the class in which it is declared. This means that only one thread can be executing any part of the method at any one time. However, I understand that the Java Runtime itself sometimes synchronises on this Class object for its own reasons, therefore you might sometimes find a synchronised static method blocks when no other thread is executing it. Usually better, therefore, to explicitly synchronise on some object or other, than to use synchronised static methods.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: static methods and threads
 
Similar Threads
Object Lock and a class level lock
What is the logic behind calls like JOptionPane.method()
static synchronized methods
What's the use of having a synchronized static method?
static synchronization