Re: updating an ImageIcon's backing BufferedImage content from multiple
John B. Matthews wrote:
Also, I don't see an explicit guarantee of thread safety,
something that also vexes me, since I assume that all methods in
javax.swing.* require this guarantee or they are not safe.
I may reading too much into "happen after all pending events are
processed." I thought the "call to start on a thread happens-before any
action in the started thread" was sufficient.
One thing I did miss: You linked to java.awt.EventQueue.invokeLater(),
not the Swing version. Swing's version does have an explicit guarantee.
Unfortunately, SwingUtilities.invokeAndWait() sorta doesn't, you have
to read between the lines a little.
(invokeAndWait() does say that the method should be used by other
threads to execute code on the EDT; it doesn't actually say that it's
safe to do so. One had to infer that it's safe. Not much of a stretch,
but I like complicated issue like threading spelled out so I can't
possibly make a mistake.)
Anyway, the bit about "happen after all pending events are processed" to
me just implies that somewhere there's a queue with a lot of Runnables
in it. The EDT just cycles around picking up Runnables from that queue
and executing them. The EDT is not crated fresh with a start() each
time one Runnable is presented; the same thread is reused.
There's no rational way to do that except to synchronize the queue; it
would obviously be not thread safe and probably there'd be no way to
make it so. Therefore, that queue is effectively the shared lock. Your
thread puts Runnable in and the EDT takes them out, thus achieving
synchronization. But the explicit guarantees in the docs are important
so that programmers know what they can rely on.