I currently have a scheduled executor which sends out a message after a delay like this:
executor.schedule(new Runnable() {
public void run() {
emitter.emit( message );
}
}, delay, TimeUnit.MILLISECONDS);
I need to have another thread which will listen for a cancel message which will send a different message and I need to stop the above message from sending. If no cancel message is received the above sends as normal. Whats the best way to do this?
Thanks.
This can be done by using the return value of
ScheduledFuture<?> future = executor.schedule(new Runnable() {
public void run() {
emitter.emit( message );
}
}, delay, TimeUnit.MILLISECONDS);
future.cancel(true); //true if task should be interrupted
or by using the shutdown() method of the executor.
Take a look at shutdown()
and cancel()
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