hashmap - Java visibility: final static non-threadsafe collection changes after construction -
i found following code snippet in luaj , started doubt if there possibility changes made map after has been constructed might not visible other threads since there no synchronization in place.
i know since map declared final, initialized values after construction visible other threads, changes happen after that.
some might realize class not thread-safe calling coerce in multi-threaded environment might cause infinite loop in hashmap, question not that.
public class coercejavatolua { static final map coercions = new hashmap(); // map visible threads after construction, since final public static luavalue coerce(object paramobject) { ...; if (localcoercion == null) { localcoercion = ...; coercions.put(localclass, localcoercion); // visible? } return ...; } ... }
you're correct changes map may not visible other threads. every method accesses coercions (both reading , writing) should synchronized on same object. alternatively, if never need sequences of accesses atomic, use synchronized collection.
(btw, why using raw types?)
Comments
Post a Comment