取消预定的执行者

编程入门 行业动态 更新时间:2024-10-11 19:24:20
本文介绍了取消预定的执行者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前有一个预定的执行器,它在像这样的延迟后发出一条消息:

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?

谢谢.

推荐答案

这可以通过使用

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()方法. 看看关机 a>() 和取消 ()

or by using the shutdown() method of the executor. Take a look at shutdown() and cancel()

更多推荐

取消预定的执行者

本文发布于:2023-11-25 13:07:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:执行者

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!