取消预定的执行者 [英] Cancelling scheduled executor

查看:78
本文介绍了取消预定的执行者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()

这篇关于取消预定的执行者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆