I have a use case based on a GUI problem that I would like to convey to your insight.
Use case
I have a graphical interface that displays the calculation result depending on some parameters set by the user in the graphical interface. For example, when a user moves the slider, several events are triggered that trigger a new calculation. When the user adjusts the value of the slider from A to B, a dozen events are triggered.
But the calculation can take up to several seconds, while adjusting the slider can trigger an event every few seconds.
How to write the correct thread that will listen to these events, and how to filter them so that the redrawing of the results is live? Ideally, you would like something like
- start a new calculation as soon as the first change event is received;
- cancel the first calculation if a new event is received, and start a new one with new parameters;
- but make sure that the last event will not be lost, because the last completed calculation should be the last with the last updated parameters.
What i tried
My friend (A. Cardona) proposed this low-level approach in the Updater thread, which prevents too many events from causing a calculation. I will copy it here (GPL):
He puts this in a class that extends Thread:
public void doUpdate() { if (isInterrupted()) return; synchronized (this) { request++; notify(); } } public void quit() { interrupt(); synchronized (this) { notify(); } } public void run() { while (!isInterrupted()) { try { final long r; synchronized (this) { r = request; }
Each time an event is dispatched by the GUI, indicating that the parameters are changed, we call updater.doUpdate() . This causes the refresh() method to be called much less. But I do not control it.
Another way?
I was wondering if there is another way to do this to use the jaca.concurrent classes. But I could not sort within Executors, where should I start.
Do any of you have experience with similar uses?
thanks
Jean-yves
source share