Trying do some debouncing, the use case is that client may send multiple requests to server in short interval unnecessarily. The requests are all the same but response is different at the time of the server processing, so the last response is the one to use.
With following code if do not always create a new timer, after timer.cancel(), the timer.schedule() will crash with "Timer already cancelled.".
Can't the timer be reused?
Is there better way to do the debouncing without using timer?
private Timer debouncerTimer = new Timer();
synchronized void debounceRequest() {
debouncerTimer.cancel();
//debouncerTimer = new Timer(); //<== without this one it crashes with IllegalStateException("Timer already cancelled.")
debouncerTimer.schedule(new TimerTask() {
@Override
public void run() {
doSendRequest();
}
}, 150);
}
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Timer.html
Here is an example using ScheduledExecutorService that could work: