我目前有一个预定的执行程序,它会在延迟后发出一条消息,如下所示:
executor.schedule(new Runnable() {
public void run() {
emitter.emit( message );
}
}, delay, TimeUnit.MILLISECONDS);
我需要另一个线程来侦听取消消息,该消息将发送不同的消息,并且我需要停止发送上述消息。如果没有收到取消消息,则上述内容将正常发送。最好的方法是什么?
Thanks.
这可以通过使用返回值来完成
ScheduledFuture<?> future = executor.schedule(new Runnable() {
public void run() {
emitter.emit( message );
}
}, delay, TimeUnit.MILLISECONDS);
future.cancel(true); //true if task should be interrupted
或者使用执行器的 shutdown() 方法。
看一眼shutdown()
and cancel()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)