Win a copy of Securing DevOps this week in the Security forum!

N Fel

Greenhorn
+ Follow
since Mar 24, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
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 N Fel

That sounds good, but does that mean I'd have the same issue if the method was not static? Synchronizing the method wouldn't help either, right?

How could I make it thread safe?
I've read a lot on threads, but have yet to see an answer for the following.

Is a static method thread-safe if:
1.) it's not accessing any static variables, and
2.) it's not modifying objects passed in?

I have a utility class for reporting which has static methods that accept a List. I'm wondering if it should be synchronized and why. Multiple users run reports simultaneously, so the same method can be called simultaneously by multiple threads (each passing in a different Vector, ArrayList, or Set). Here's a sample method in my utility class:

public static Double calculateTotal(Collection myItems) {
double dTotal = 0.0;

Iterator theIterator = myItems.iterator();

while (theIterator.hasNext()) {
MyItem theItem = (MyItem) theIterator.next();
dTotal += theItem.price;
}

Double theTotal = new Double(dTotal);
return theTotal;
}

Thanks so much for any insight you can offer.