This is related to an earlier question I asked, where the answer was:
If a field is accessed by multiple threads, it should be volatile or final, or accessed only with synchronized blocks. Otherwise, assigned values may not be visible to other threads.
In addition anything that manipulates pixels on screen should be run from the event dispatch thread although this is handled transparently when you use repaint / paint.
Therefore, by my understanding, we need worry about the memory model for something as simple as an animation of a sprite moving across the screen.
My question is, is this understanding correct, and Sun tutorial examples eg TumbleItem (source) incorrect?
You know, I think you may have a point here. The TumbleItem code uses
worker.isDone()
to find out whether the work has been completed. However, I don't think this causes a full "synchronize".My reading of the JDK 1.6 code is that
SwingWorker.isDone()
usesFutureTask
which in turn uses aSync
object with a volatilestate
attribute. The execution path forisDone()
doesn't seem to involve asynchronized
method or block.I'm not a real expert on the new concurrent classes, but I think the TumbleItem should be calling
worker.get()
at some point to guarantee proper synchronization.EDIT: I'm referring to the EDT's use of the
img
array that is populated by the worker. However, it also appears there is an issue with the EDT's use of the initialization parameters, as noted by @The Feast.