Re: Synchronization when collecting data from the EDT?
In article <rqZHp.2350$5v5.528@newsfe11.iad>,
Knute Johnson <nospam@knutejohnson.com> wrote:
On 06/07/2011 10:38 AM, John B. Matthews wrote:
I can't find a Sun/Oracle example using invokeAndWait() dated later
than 2000, well after the advent of java.util.concurrent. Would it
be correct to infer that using, say BlockingQueue, would obviate
the need for invokeAndWait()?
There is a happens before relationship in a blocking queue. Putting
an element in the queue happens before retrieving it from the queue.
You could synchronize that way as well. All actions before placing
an element in the queue in one thread would be visible in another
after removing that element from the queue.
Agreed. In the variation below, a LinkedBlockingQueue would allow one to
schedule a sample on the EDT via invokeLater().
public void startUtilTimer() {
final Queue<Integer> samples = new LinkedBlockingQueue<Integer>();
java.util.Timer sampler = new java.util.Timer();
sampler.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
while (samples.size() > 5) {
samples.remove();
System.out.println(samples);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
samples.add(getValue());
}
});
}
}, 1000, 1000);
}
Compare to the invokeAndWait() variation:
<https://groups.google.com/d/msg/comp.lang.java.gui/sWKy5Oo8s3E/xaGY8-VoMKYJ>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>