Link to home
Start Free TrialLog in
Avatar of yuvalg
yuvalg

asked on

concurrent access to static methods

Hey,
What happens during concurrent access (from different threads) to a static method?

Do I need to synchronize the method in order to prevent a mess?

yuval.
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Cakalic
There is no automatic multi-threaded protection inherent to a static method. A static method is just like an instance method except that it operates on a set of fields for which there can only be one instance in the JVM.

As for whether synchronization is needed or not, this comes down to whether the method is changing the state of static data on which it operates and whether it does so atomically. Atomic operations are those that the VM can guarantee will be non-interruptible.

As you might guess, there ain't a lot of guaranteed atomic operations. Unless you are operating exclusively with primitive datatypes (excluding double and long) and performing simple single VM instructions like pre-increment (++), what you are doing is not atomic. Even incrementing a long value is not atomic as it requires more than one VM instruction to implement. That means it would be possible (even if not highly probable) that a thread in the middle of changing the value of the long could be interrupted by another thread which, retrieving the value of the same long, now gets a corrupt value.

So ... good for you for thinking about multi-threaded issues. If you believe the best design is to have static methods but the underlying class fields on which these operate are not immutable, then by all means synchronize the methods or synchronize on one or more of the object fields as needed. Synchronizing methods will force single-threading through all the synchronized methods of the class. Synchronizing on object fields will allow concurrent thread access to discrete subsets of the class data. However, beware this because you can introduce thread deadlock.

Hope I haven't scared you off completely. Threading is a powerful Java capability. Powerful both in solving problems and introducing new ones.

Best regards,
Jim Cakalic

Avatar of yuvalg
yuvalg

ASKER

I get it.
Your main idea of static method is not accurate though.
 As i understand it, static methods are not solely dedicated to accessing static methods.
They are used to perform "any non class-variable manipulation" as done in the java Integer class.

What still remains a mistery (to me :) is how to declare static methods within an interface.
Buts thats another question and ill offer points after i do some digging of my own.

Thanks all.

You're quite right, static methods are note SOLELY for the purpose of accessing static methods. But since your question was about static (presumably as opposed to instance) methods, my answer referenced a static methods TENDENCY to access other static methods and variables. You'll note that the last line:

"So, yes, if your static method changes the value of global variables, or even member variables of existing classes..."

does in fact acknowledge the possibility. I didn't explore it in detail since it is a side issue to synchronization and threadsafety, in the sense that whether a static method accesses member variables or not doesn't change the answer about the need for synchronization. My apologies for any confusion I caused.