English 中文(简体)
我需要关闭一个计划外执行者服务, 但需要时需要启动它
原标题:I Need To Shutdown A ScheduledExecutorService, But Need To Start It Up When Needed

我做了一个甜蜜的系统更新功能 这个游戏我在这里做的是代码:

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;
        时 时
    时 时

时 时

时 时

s 这样, 当系统更新计数器达到0时, 服务器将重新启动自己。 它的工作很好, 但问题从这里开始

    case "update":
        if (Short.parseShort(txtSystemUpdate.getText()) != 0) {
            SystemUpdateHandler.setUpdateSeconds(Short.parseShort(txtSystemUpdate.getText()));
            SystemUpdateHandler.refresh();
            if (!SystemUpdateHandler.running) {
                SystemUpdateHandler.start();
            时 时
        时 时 else {
            SystemUpdateHandler.stop();
            for (Player p : Static.world.players){ 
                if (p.ready()) {
                    ActionSender.sendSystemUpdate(p, 0);
                时 时
            时 时
        时 时
        break;

这就是我称之为它的地方, 基本上, 如果我输入一个数字高于 0, 程序就运作正常。 但是如果输入数字 0, 调度器将停止运行( 保存内存), 因为不需要它, 除非我发送系统更新 。 基本上, 我输入 0 时如何阻止调度器运行, 但当我输入一个数字 & gt; 然后 0( 几次) 时, 我能够重新启动它 。

最佳回答

一旦关闭执行器服务无法重新开始, 请从变量声明( 并删除最终声明) 中移动创建它, 然后在开始方法中这样做 :

//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();
    ...
问题回答

关闭时, 您将获得提交给调度器的任务列表, 您可以使用此列表创建新的任务列表。 调度器一旦停止无法启动 - 因为线性球已经死, 所有工人的线性线都死了 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签