Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Multithreading_Shutdown_Executorservice - Fatal编程技术网

Java 我需要关闭ScheduledExecutorService,但需要在需要时启动它

Java 我需要关闭ScheduledExecutorService,但需要在需要时启动它,java,multithreading,shutdown,executorservice,Java,Multithreading,Shutdown,Executorservice,我为这个游戏做了一个甜蜜的系统更新功能,下面是代码: public static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); private static CountDownThread countDownThread; public static boolean running = false; private static short updateSecon

我为这个游戏做了一个甜蜜的系统更新功能,下面是代码:

public static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private static CountDownThread countDownThread;
public static boolean running = false;

private static short updateSeconds;


public static void start() {
    System.out.println("starting");
    running = true;
    countDownThread = new CountDownThread();
    scheduler.scheduleWithFixedDelay(countDownThread, 0, 1000, TimeUnit.MILLISECONDS);
}

public static void stop() {
    System.out.println("Stoping");
    scheduler.shutdown();
    running = false;
    updateSeconds = 0;
    System.out.println("Stopped");
}

public static void refresh() {
    for (Player p : Static.world.players){ 
        if (p.ready()) {
            if (updateSeconds > 0) {
                ActionSender.sendSystemUpdate(p, updateSeconds+1);
            } else {
                ActionSender.sendSystemUpdate(p, updateSeconds);
            }
        }
    }
}

public static short getUpdateSeconds() {
    return updateSeconds;
}

public static void setUpdateSeconds(short updateSeconds) {
    SystemUpdateHandler.updateSeconds = (short) (updateSeconds);
}

public static class CountDownThread implements Runnable {

    @Override
    public void run() {
        System.out.println(updateSeconds);
        updateSeconds--;
        if (updateSeconds <= 0) {
            Static.server.restart();
            scheduler.shutdown();
            running = false;
        }
    }

}

这就是我调用它的地方,基本上,如果我输入一个大于0的数字,程序工作正常。但是我想要它,所以如果我输入数字0,调度程序将停止运行(以节省内存),因为它不需要,除非我发送系统更新。基本上,当我输入0时,如何停止计划程序运行,但当我输入一个数字>然后输入0(多次)时,如何启动计划程序备份。

关闭时,您将获得提交给计划程序的任务列表,您可以使用此列表创建新的任务列表。一旦停止,计划程序就无法启动-因为线程池已死亡,并且所有工作线程也已死亡。

关闭时,您将获得提交给计划程序的任务列表,您可以使用此列表创建新的任务列表。一旦停止,计划程序就无法启动-因为线程池已死亡,并且所有工作线程也已死亡。

一旦关闭,ExecutorService将无法再次启动,因此请从变量声明中移动创建它的操作(并删除final),改为在start方法中执行该操作:

//not static and not final, normal instance variable instead:
public ScheduledExecutorService scheduler;
...

//and create it in the start method isntead:
public static void start() {
    System.out.println("starting");
    scheduler = Executors.newSingleThreadScheduledExecutor();
    ...

关闭后,ExecutorService将无法再次启动,因此请从变量声明(和remove final)中移动它的创建,并改为在start方法中执行该操作:

//not static and not final, normal instance variable instead:
public ScheduledExecutorService scheduler;
...

//and create it in the start method isntead:
public static void start() {
    System.out.println("starting");
    scheduler = Executors.newSingleThreadScheduledExecutor();
    ...

我认为你正在使用一种非常复杂的方式,只是将任务延迟N秒。为什么不安排它在n秒后运行,而不是一秒一秒地倒计时?顺便说一句,我不会麻烦启动/停止单个线程。让它一直萦绕不去。我认为你正在使用一种非常复杂的方式,只是将你的任务延迟N秒。为什么不安排它在n秒后运行,而不是一秒一秒地倒计时?顺便说一句,我不会麻烦启动/停止单个线程。让它一直停留。如果不在stop方法中关闭ExecutorService,则需要在某处调用shutdown,否则进程将永远不会终止。如果不在stop方法中关闭ExecutorService,则需要在某处调用shutdown,否则进程将永远不会终止。