是否有检查计时器是否为 运行 的代码?
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 计时器会停止 运行ning。为了再次使其 运行,我添加了一段代码,使其在您重新登录后再次执行 startTimer()。但是,由于如果服务器未注销,计时器仍然 运行s,因此计时器启动 运行ning 两次。计时器开始减去 2 或更多,具体取决于您注销和登录的次数。我认为如果有代码可以确定计时器是否已经 运行ning,它会修复。有没有办法做到这一点?请帮忙!
我在提供检查 TimerTask 对象 (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/TimerTask.html) 状态的文档中没有看到任何内容,因此一种选择是扩展 TimerTask 并创建您自己的 class。除了使用匿名 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 检查此对象。
public 长 scheduledExecutionTime()
Returns the scheduled execution time of the most recent actual execution of this task. (If this method is invoked while task execution is in progress, the return value is the scheduled execution time of the ongoing task The return value is undefined if the task has yet to commence its first execution.
This method is typically not used in conjunction with fixed-delay execution repeating tasks, as their scheduled execution times are allowed to drift over time, and so are not terribly significant.
- 第一件事定期 运行 任务需要 set/reset 状态标志
- 其次(看例子的时候)这种class
最好封起来
但是如果有人坚持要这样的方法
public abstract class NonInterruptableTask extends TimerTask {
protected boolean isDone = false;
public boolean isDone() {return isDone;}
protected abstract void doTaskWork();
@Override
public void run() {
isDone = false;
doTaskWork();
isDone = true;
}
}
用法:
TimerTask myTask = new NonInterruptableTask() {
@Override
public void doTaskWork() {
//job here
}
};
您还可以声明一个名为 "timerstate" 之类的布尔状态,并使其默认为 false。每当您启动计时器时,您都可以将此布尔值更改为 true,这样您就可以跟踪计时器。
public boolean timerstate;
public Timer t1;
// some code goes here to do whatever you want
if(timerstate == true) {
t1.cancel();
t1.purge();
t1 = new Timer();
} else{
t1.schedule(new TimerTask() {
@Override
public void run() {
timerstate = true;
//rest of code for the timer goes here
}
}
}
我有一个游戏,我正在安排计时器。我有这个 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 计时器会停止 运行ning。为了再次使其 运行,我添加了一段代码,使其在您重新登录后再次执行 startTimer()。但是,由于如果服务器未注销,计时器仍然 运行s,因此计时器启动 运行ning 两次。计时器开始减去 2 或更多,具体取决于您注销和登录的次数。我认为如果有代码可以确定计时器是否已经 运行ning,它会修复。有没有办法做到这一点?请帮忙!
我在提供检查 TimerTask 对象 (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/TimerTask.html) 状态的文档中没有看到任何内容,因此一种选择是扩展 TimerTask 并创建您自己的 class。除了使用匿名 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 检查此对象。
public 长 scheduledExecutionTime()
Returns the scheduled execution time of the most recent actual execution of this task. (If this method is invoked while task execution is in progress, the return value is the scheduled execution time of the ongoing task The return value is undefined if the task has yet to commence its first execution.
This method is typically not used in conjunction with fixed-delay execution repeating tasks, as their scheduled execution times are allowed to drift over time, and so are not terribly significant.
- 第一件事定期 运行 任务需要 set/reset 状态标志
- 其次(看例子的时候)这种class 最好封起来
但是如果有人坚持要这样的方法
public abstract class NonInterruptableTask extends TimerTask {
protected boolean isDone = false;
public boolean isDone() {return isDone;}
protected abstract void doTaskWork();
@Override
public void run() {
isDone = false;
doTaskWork();
isDone = true;
}
}
用法:
TimerTask myTask = new NonInterruptableTask() {
@Override
public void doTaskWork() {
//job here
}
};
您还可以声明一个名为 "timerstate" 之类的布尔状态,并使其默认为 false。每当您启动计时器时,您都可以将此布尔值更改为 true,这样您就可以跟踪计时器。
public boolean timerstate;
public Timer t1;
// some code goes here to do whatever you want
if(timerstate == true) {
t1.cancel();
t1.purge();
t1 = new Timer();
} else{
t1.schedule(new TimerTask() {
@Override
public void run() {
timerstate = true;
//rest of code for the timer goes here
}
}
}