Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refactoring a static method for use in a clustered environment(multiple threads)

 
Janarthan S Sathiamurthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chaps,

I have a static method in a class. This static method is currently being accessed by various(huge in number) classes. Now we are planning to operate this code in a clustered environment.
The code was -
public static String getString(String parameter1, String parameter2) {
// Process a logic thats specific to the parameters passed in
return "processed value";
}

So, I need to refactor this method. My refactored code goes as -

public class RefactoredClass {

public static String getString(String parameter1, String parameter2) {
return new RefactoredClass().getStringModified(parameter1, parameter2);
}
public String getStringModified(String parameter1, String parameter2) {
// Process a logic thats specific to the parameters passed in
return "processed value";
}
}

Basically I want each of the callee class to work on an individual reference.
Guys, Can you please advice on this approach. How would this behave in a clustered environment with mutiple threads accessing the refactored static method.

Best regards,
Janarthan S
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch.

Note that there's nothing at all wrong with using static methods in a multithreaded environment; it's static data that could cause a problem. You have to be careful when threads share data, but there's absolutely no problem with any number of threads calling the same static method (or the same instance method on the same object) at once.

Moving the method into a class, then creating an instance and calling the method from the static method accomplishes nothing at all, unless the static method goes with some static member variables, which you are turning into instance variables in your new class.
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What he's saying, is creating variables inside of a static method is not thread-save, unless that part of it is synchronized.

Since your static method has two parameters, it automatically creates instance variables, and the method must be synchronized to operate correctly.

Your solution won't work either - consider the following:

Stage 1:


Stage 2:


Delegating to another method only makes it a little less efficient (read: VERY VERY LITTLE DIFFERENCE) - but it doesn't accomplish your goal.

Making the method synchronized is your best way to buy some time - if it's been in production and it's now going to a clustered environment, I wouldn't think this method is your bottleneck.

I'd like to see what others have to say.
 
Janarthan S Sathiamurthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I see three ways to get rid of this problem -
1. Synchronize the code inside static method.
2. Make the class containing the static method implement a Prototype pattern(returning a cloned reference of self to each callee), make the method non-static. Thus on the returned cloned reference, each callee will invoke the "now non-static method" and get the work done.
3. Make the method non-static and each callee will invoke new Class().method()

Synchronizing the code inside static method seems a bottleneck, as this static method in this class will be called simultaneously by huge number of classes

Though I need to modify many classes, Point 2/3 looks more sensible. Guys Please advice which of this is a clean way to accomplish this. Still thinking whether there is any other cleaner way :roll:

Best regards,
Janarthan S
[ October 30, 2007: Message edited by: Janarthan S Sathiamurthy ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam Schaible:
What he's saying, is creating variables inside of a static method is not thread-save, unless that part of it is synchronized.

Since your static method has two parameters, it automatically creates instance variables, and the method must be synchronized to operate correctly.


I'm sorry, Adam, but all of this is completely wrong. I'm not even sure where to start to explain what's wrong with it.

In general, creating objects referenced by parameters or local variables inside any kind of method (static or not) is thread-safe. Parameters and local variables are stored on the "stack", and each thread has its very own stack; therefore there's no interference at all.

"Instance variables" are the data members of an object that are not static. In no way, shape, or form is there any relationship whatsoever between the parameters to a static method (or any other method) and instance variables (or static members, or any kind of member variables!)

Janarthan, please ignore the above -- it's not going to help you!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janarthan,

As I said, I don't see any evidence that you need to do anything, unless your static method accesses some shared data structure. Does it?
 
Janarthan S Sathiamurthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had a relook at the code. The code inside the static method doesn't share any data. It contains all method level(local) variables only.
So, even if multiple threads access this method at a given point of time, the code would work without any issues.. i.e., it would be thread-safe.

Basically, Can i conclude that accessing a static method like below -

public Class SharedAction {

public static String performUtiliy() {
// This method don't use any class level variable inside
String localVar = "test variable";
.
.
.
return "success";
}

}

in a multithreaded environment in completely safe ?

Please advice.

Best regards,
Janarthan S
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, absolutely.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic