On 6/4/2011 5:38 PM, Knute Johnson wrote:
If you want to remove some data from the EDT and use it in another
thread, does EventQueue.invokeLater() or invokeAndWait() constitute
happens before? I don't think it does,
Yes and no.
You are correct, SwingUtilities.invokeLater() does NOT specify that it
creates a happens-before relationship. I've griped about this before on
this group. I think it should, because it's almost impossible to use
correctly if it does not.
However, if you look at the implementation, you can see it uses an
Executor to hand off your runnable to the EDT. And the Executor method
invoked does, in fact, specify a happens before relationship. And I
can't think of any way to hand off one object (the runnable) to another
thread with out creating a happens-before relationship. So as a
practical matter, I think you must be ok if you don't synchronize yourself.
If feel you must synchronzie (and I don't disagree), the easiest way
might be to just create a volatile field in the Runnable and let that
synchronize for you.
class MyRunnable implements Runnable {
public volatile int synch;
public void run() {
synch++; // read synch
// do stuff here...
}
}
...
MyRunnable run = new MyRunnable();
run.synch = 42;
SwingUtilities.invokeLater( run );
...
This is the best I think I can do.
I think that would work too.