Solved

concurrent access to static methods

Posted on 2000-04-11
4
2,989 Views
Last Modified: 2009-07-25
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.
0
Comment
Question by:yuvalg
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 2704554
In general, any method that changes state information (i.e. non local variables) needs to be synchronized in order to be thread safe. The only difference between a "regular" method and a static method, is that a static method tends to access static (global) variables, and it must be synchronized in a global, not an instance, fashion.

So, yes, if your static method changes the value of global variables, or even member variables of existing classes, you will need to synchronize it. If it purely and only manipulates local values (such as Integer.parseInt would do for instance), then it is not necessary.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2704592
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

0
 
LVL 3

Author Comment

by:yuvalg
ID: 2705302
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.

0
 
LVL 16

Expert Comment

by:imladris
ID: 2705386
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.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 31
web project error add remove 1 27
jsp error 6 25
junit as external jar or library 7 22
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question