Solved

concurrent access to static methods

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 84
Java - Why doesn't this JFrame work 3 58
Basic Java Case or If-Else statement... 3 50
Windows 10 IE Certificate Issue 10 45
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

813 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

14 Experts available now in Live!

Get 1:1 Help Now