Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关闭ScheduledExecutorService_Java_Scheduled Tasks_Discord_Timertask_Scheduledexecutorservice - Fatal编程技术网

Java 关闭ScheduledExecutorService

Java 关闭ScheduledExecutorService,java,scheduled-tasks,discord,timertask,scheduledexecutorservice,Java,Scheduled Tasks,Discord,Timertask,Scheduledexecutorservice,所以我有一个discord机器人,我试图每小时发送一条消息。我用预定的执行人服务让这部分工作起来。从我说开始!未来 我想在发送邮件时阻止它!未来 但是,我无法让它正常工作。这是我的密码: String[] args = event.getMessage().getContentRaw().split(" "); if (args[0].equalsIgnoreCase("!futuresOn")) { Color red = new

所以我有一个discord机器人,我试图每小时发送一条消息。我用预定的执行人服务让这部分工作起来。从我说开始!未来

我想在发送邮件时阻止它!未来

但是,我无法让它正常工作。这是我的密码:

    String[] args = event.getMessage().getContentRaw().split(" ");
    if (args[0].equalsIgnoreCase("!futuresOn")) {
    Color red = new Color(255, 0, 0);
    Color green = new Color(0, 204, 0);
       Runnable futRun = new Runnable() {
       public void run() {
        EmbedBuilder futuresBot = new EmbedBuilder();
        futuresBot.setAuthor("Futures Bot", null, event.getAuthor().getAvatarUrl());
   
        try {
    futuresBot.addField("**S&P 500**", getSPY(), false);
    futuresBot.addField("**NASDAQ**", getNASDAQ(), false);
    futuresBot.addField("**DOW**", getDOW(), false);
    if (getSPY().contains("+")) {
    futuresBot.setColor(green);
    } 
    else {
    futuresBot.setColor(red);
    }
        }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
   
   
        futuresBot.setFooter("Cantina Capital", event.getGuild().getIconUrl());
        event.getChannel().sendMessage(futuresBot.build()).queue();
   
       }
      
    };
    
    exec.scheduleAtFixedRate(futRun, 0, 1, TimeUnit.HOURS);
    
    
    }
    if (args[0].equalsIgnoreCase("!futuresoff")) {
    event.getChannel().sendMessage("Futures bot off.").queue();
    exec.shutdownNow();
    }
我尝试过几种不同的方法,但我不能让它正常工作。快到终点了!未来之所以如此,部分是因为它发送了“未来机器人关闭”消息。我只是希望它能够发送消息时,每小时一次!futureson已发送,而不是何时发送!“未来”已发送。我也试过了
ScheduledThreadPoolExecutor exec=新的ScheduledThreadPoolExecutor(0)


如果有人能为我指明正确的方向,我将不胜感激。

首先,您应该将
scheduleAtFixedRate()
的结果保存在Listener类中

ScheduledFuture scheduledFuture = null;
然后您可以在此对象上使用
cancel()
来停止它。 您还应该在调度之前测试它是否为null,并在取消之前测试它是否已设置:

String[] args = event.getMessage().getContentRaw().split(" ");
if (args[0].equalsIgnoreCase("!futuresOn")) {
    Color red = new Color(255, 0, 0);
    Color green = new Color(0, 204, 0);
    Runnable futRun = new Runnable() {
        public void run() {
            EmbedBuilder futuresBot = new EmbedBuilder();
            futuresBot.setAuthor("Futures Bot", null, event.getAuthor().getAvatarUrl());

            try {
                futuresBot.addField("**S&P 500**", getSPY(), false);
                futuresBot.addField("**NASDAQ**", getNASDAQ(), false);
                futuresBot.addField("**DOW**", getDOW(), false);
                if (getSPY().contains("+")) {
                    futuresBot.setColor(green);
                }
                else {
                    futuresBot.setColor(red);
                }
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            futuresBot.setFooter("Cantina Capital", event.getGuild().getIconUrl());
            event.getChannel().sendMessage(futuresBot.build()).queue();

        }

    };

    if (scheduledFuture == null)
        scheduledFuture = exec.scheduleAtFixedRate(futRun, 0, 1, TimeUnit.HOURS);


}
if (args[0].equalsIgnoreCase("!futuresoff")) {
    event.getChannel().sendMessage("Futures bot off.").queue();
    if (scheduledFuture != null) {
        scheduledFuture.cancel(true);
        scheduledFuture = null;
    }
}
    ```

似乎您需要使用取消,cf.duplicate