Originally posted by Someone Else:
...This works up to a point, but sling more than a couple of objects in the arraylist and I get this .....
"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."
Originally posted by Mark Hedge:
Alright, I got it working! - its a little different from the post above, though.
I had to do this first (gameobjects is of type List)
gameobjects = Collections.synchronizedList(new ArrayList());
the synchronized had to go on the collection not the object being drawn from the collection
I would note that it's pretty rare in my experience that the alleged "thread safety" you get from Collections.synchronizedXXX(), or from Vector and Hashmap, is good enough to actually make your code thread-safe. Typically there are places where you need to prevent other threads from cutting in in between two different synchoronized method invocations - this means you need additional synchronization outside the methods themselves. And if you're going to do that, why bother using synchronizedXXX() at all? It just confuses the issue by putting some synchronization inside the XXX class, while other synchronization is in the class that uses the XXX. Just put it all in the class that uses it. Many, many bugs have been committed by people relying on the false promise of "thread safety" from synchronizedXXX() etc.
"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."
"I'm not back." - Bill Harding, Twister
public static <T> List<T> synchronizedList(List<T> list)
Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list.
It is imperative that the user manually synchronize on the returned list when iterating over it:
List list = Collections.synchronizedList(new ArrayList());
...
synchronized(list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
Failure to follow this advice may result in non-deterministic behavior.
The returned list will be serializable if the specified list is serializable.
Parameters:
list - the list to be "wrapped" in a synchronized list.
Returns:
a synchronized view of the specified list.
"I'm not back." - Bill Harding, Twister
"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."
Originally posted by Mark Hedge:
Am I right in assuming that as that is not iterating through the collection I dont need to use a synchronized codeblock there for gameobjects?
Using synchronizedList() may be an acceptable way to achieve the same effect, assuming the other code is not trying to do anything overly complex (like, say, iterating).
The only other place where gameobjects is being used at the moment after initialisation is here, in part of a case statement:
"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."
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|