是否有代码检查计时器是否正在运行? [英] Is there a code to check if a timer is running?

查看:63
本文介绍了是否有代码检查计时器是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计划计时器的游戏.我有这个CoresManager文件:

I have a game where I am scheduling a timer. I have this CoresManager file:

package com.rs.cores;

 import java.util.Timer;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;

 public final class CoresManager {

protected static volatile boolean shutdown;
public static WorldThread worldThread;
public static ExecutorService serverWorkerChannelExecutor;
public static ExecutorService serverBossChannelExecutor;
public static Timer fastExecutor;
public static ScheduledExecutorService slowExecutor;
public static int serverWorkersCount;

public static void init() {
    worldThread = new WorldThread();
    int availableProcessors = Runtime.getRuntime().availableProcessors();
    serverWorkersCount = availableProcessors >= 6 ? availableProcessors - (availableProcessors >= 12 ? 7 : 5) : 1;
    serverWorkerChannelExecutor = availableProcessors >= 6 ? Executors
            .newFixedThreadPool(availableProcessors - (availableProcessors >= 12 ? 7 : 5),
            new DecoderThreadFactory()) : Executors.newSingleThreadExecutor(new DecoderThreadFactory());
    serverBossChannelExecutor = Executors
            .newSingleThreadExecutor(new DecoderThreadFactory());
    fastExecutor = new Timer("Fast Executor");
    slowExecutor = availableProcessors >= 6 ? Executors.newScheduledThreadPool(availableProcessors >= 12 ? 4 : 2,
                    new SlowThreadFactory()) : Executors
            .newSingleThreadScheduledExecutor(new SlowThreadFactory());
    worldThread.start();
}

public static void shutdown() {
    serverWorkerChannelExecutor.shutdown();
    serverBossChannelExecutor.shutdown();
    fastExecutor.cancel();
    slowExecutor.shutdown();
    shutdown = true;
}

private CoresManager() {

}
}

我正在游戏中使用它:

    private void startTimer() {
    CoresManager.fastExecutor.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            if (timer == 0 || timer < 1) {
                player.sm("Your timer has ended! The NPCs will no longer spawn.");
                timer = 0;
                this.cancel();
                exitInstance(); 
                return;
            }
            timer--;
            timerchecker = true;
            seconds = timer % 60;
            player.setTimer(timer);
            minutes = TimeUnit.SECONDS.toMinutes(timer);            
        }
    }, 0, 1000);
}

如果播放器注销并且服务器重新启动,则CoresManager计时器将停止运行.为了使其再次运行,我添加了代码以使其在您重新登录后再次执行startTimer().但是,由于如果服务器未注销,计时器仍在运行,因此计时器开始运行两次.根据您注销和登录的次数,计时器开始减去2或更多.我认为,如果有代码确定计时器是否已在运行,它将解决.有没有办法做到这一点?请帮忙!

The CoresManager Timer stops running if the player logs out AND the server gets rebooted. To make it run again, I added a code to make it do startTimer() again once you log back in. However, since the timer still runs if the server didn't log out, the timer starts running twice. The Timer starts getting subtracted by 2, or more, depending on how many times you log out and in. I figure that it would fix if there was a code to determine if the timer is already running. Is there a way to do this? Please help!

推荐答案

我在文档中看不到任何用于检查TimerTask对象状态的文档(

I don't see anything in the documentation that provides for checking the status on a TimerTask object (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/TimerTask.html) so one option would be to extend TimerTask and create your own class. Instead of using an anonymous TimerTask, you could create something along the lines of:

public class CoresTimerTask extends TimerTask {

    private boolean hasStarted = false;

    @Overrides
    public void run() {
        this.hasStarted = true;
        //rest of run logic here...
    }

    public boolean hasRunStarted() {
        return this.hasStarted;
    }
}

,只需维护对此CoresTimerTask对象的引用,然后将其传递给startTimer().然后,您可以稍后通过hasRunStarted检查该对象.

and just maintain a reference to this CoresTimerTask object, which you then pass into startTimer(). You can then later check this object via hasRunStarted.

这篇关于是否有代码检查计时器是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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