gpt4 book ai didi

java - 取消预定的执行者

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:34 24 4
gpt4 key购买 nike

我目前有一个预定的执行程序,它会在这样的延迟后发送一条消息:

        executor.schedule(new Runnable() {
public void run() {
emitter.emit( message );
}
}, delay, TimeUnit.MILLISECONDS);

我需要有另一个线程来监听取消消息,该消息将发送不同的消息,我需要停止发送上述消息。如果没有收到取消消息,则上述发送正常。执行此操作的最佳方法是什么?

谢谢。

最佳答案

这可以通过使用

的返回值来完成
ScheduledFuture<?> future = executor.schedule(new Runnable() {
public void run() {
emitter.emit( message );
}
}, delay, TimeUnit.MILLISECONDS);
future.cancel(true); //true if task should be interrupted

或者使用executor的shutdown()方法。
看看shutdown ()和 cancel ()

关于java - 取消预定的执行者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7178205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com