Solved

concurrent access to static methods

Posted on 2000-04-11
4
2,991 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

739 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