I have a Swing application that uses a Java Thread to constantly perform some operation. The results of this operation update the contents of a graph in the UI:
class ExampleThread {
...
public void run() {
while (running) {
// extract some information
...
// show results in UI
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// use information to update a graph
}
});
// sleep some seconds
...
}
}
...
}
The problem that I am having is when the EDT is bloated in other operations. In this case, the ExampleThread
may register various Runnable
instances that update the graph. For my application, it will be a waste of time since the graph will be updated several times before showing the results. What I would like is to run the //update a graph
code at most once per EDT cycle.
My question is: what is the proper way to ensure that a Runnable
is run only once?
An atomic dirty flag should do the trick. This has the added benefit of only adding to the EDT queue if necessary. Big assumption - your data structure(s) holding the extracted information are thread safe, but that would have to be true for your code above to work correctly as well. If they aren't you should consider the SwingWorker approach indicated in the other answer; you also could combine with the dirty flag to prevent redundant updates.
AtomicBoolean dirty = new AtomicBoolean(false);
while (running) {
// extract some information
...
if (!dirty.getAndSet(true)) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (dirty.getAndSet(false)) {
// use information to update a graph
}
}
});
}
// sleep some seconds
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With