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

编程入门 行业动态 更新时间:2024-10-28 16:28:33
是否有代码检查计时器是否正在运行?(Is there a code to check if a timer is running?)

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

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() { } }

I am using this inside the game:

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); }

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对象状态的文档中的任何内容( 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检查此对象。

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

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

更多推荐

本文发布于:2023-08-05 21:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1438096.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   正在运行   代码   code   timer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!