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?