gpt4 book ai didi

java - 是否有检查计时器是否正在运行的代码?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:25 28 4
gpt4 key购买 nike

我有一个游戏,我在其中安排计时器。我有这个 CoresManager 文件:

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 或更多,具体取决于您注销和登录的次数。我认为如果有代码来确定计时器是否已经运行,它会修复。有没有办法做到这一点?请帮忙!

最佳答案

我在提供检查 TimerTask 对象 (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/TimerTask.html) 状态的文档中没有看到任何内容,因此一种选择是扩展 TimerTask 并创建您自己的类。除了使用匿名 TimerTask,您还可以按照以下方式创建一些内容:

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 检查此对象。

关于java - 是否有检查计时器是否正在运行的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28290118/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com