Re: Synchronization when collecting data from the EDT?
On 08/06/2011 05:51, John B. Matthews allegedly wrote:
Yes, I'm arguing that invokeAndWait() alone is sufficient, but I'd
welcome an alternative using java.util.concurrent.
John, markspace, thanks for shedding some light. One remark though,
John, the JLS section you quoted pertains to final fields. In your
example, the LinkedList wasn't a field.
I believe the following section of the java.util.concurrent package doc
you referred to, to wit "Memory Consistency Properties", encompasses the
point you've been arguing around:
"An unlock (synchronized block or method exit) of a monitor
happens-before every subsequent lock (synchronized block or method
entry) of that same monitor. And because the happens-before relation is
transitive, all actions of a thread prior to unlocking happen-before all
actions subsequent to any thread locking that monitor."
I believe this means that the grabbing, off the EDT, of data produced in
a Runnable passed to #invokeAndWait() will work if, and only if, after
the Runnable is executed, the EDT locks on the same monitor that the
thread calling #invokeAndWait() is waiting on. And I suppose the problem
is that while the code does it this way, the API doesn't specify it (or
does it?). At the same time, I'm having a hard time trying to imagine
how that could possibly *not* be the case. Anyone willing to give it a go?
-.-
As for an alternative, I would humbly submit the following:
static <V> V invokeAndWait( final Callable<V> callable )
throws InterruptedException
{
final Exchanger<V> x = new Exchanger<V>();
EventQueue.invokeLater(
new Runnable(){ @Override public void run() {
try {
x.exchange( callable.call() );
}
catch (InterruptedException iex ){
Thread.currentThread().interrupt();
}
catch ( Exception e ){
//exception in the callable. Blow shit up.
throw new RuntimeException(e);
}
}}
);
return x.exchange( null );
}
static void invokeAndWait( final Runnable ron, long timeout, TimeUnit unit )
throws InterruptedException
{
final CountDownLatch l = new CountDownLatch(1);
EventQueue.invokeLater(
new Runnable(){ @Override public void run() {
ron.run();
l.countDown();
}}
);
l.await( timeout, unit );
}