Solved

concurrent access to static methods

Posted on 2000-04-11
4
2,983 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
Logs to delete in Windows VCenter server ? 11 156
count8 challlenge 13 84
Python Assistance 7 31
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now