This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!

Janarthan S Sathiamurthy

Greenhorn
+ Follow
since Oct 30, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
11
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Janarthan S Sathiamurthy

import java.util.Collections;

List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};

Collections.addAll(myList, myStringArray);

After this code, 'myList' should contain all the elements from the array.

Best regards,
Janarthan S
8 years ago
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
11 years ago
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 ]
11 years ago
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
11 years ago