Originally posted 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?
If we're going to have corrections, let's have correct ones. Yours wasn't correct, because it's the reference to the object that is the parameter, not the object itself. Read this for a full explanation:Originally posted by H. Hall:
Henry, Java passes arguments to methods by value not by reference. This means that Java passes a copy of the original object to methods.
If we're going to have corrections, let's have correct ones. Yours wasn't correct, because it's the reference to the object that is the parameter, not the object itself. Read this for a full explanation:
http://www.javaranch.com/campfire/StoryPassBy.jsp
NOW let's look at passing values to methods
Java is pass-by-value.
Always.
That means "copy the value, and pass the copy."
If each thread is working with a different instance of collection ...
once inside the static method the thread does not alter static variables and your code uses local variables that each thread has seperate....
Originally posted by Manish Khurana:
let me see if i understand you. Eevn if iterator is a local variable in a static method and so each thread has its own variable but both would point to the same Collection.
Originally posted by Henry Wong:
BTW, since the OP never posted the MyItem class, I wrote a really quick and simple one...
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
thread safe.calculateTotal
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
See where your hand is? Not there. It's next to this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
|