나는 타이머를 예약하고있는 게임을 가지고있다. 플레이어가 로그 아웃 및 서버가 재부팅 된 경우 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);
}
: 나는 게임 내에서이를 사용하고
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() {
}
}
:이 CoresManager 파일이 있습니다. 다시 실행하려면 다시 로그인하면 startTimer()를 다시 수행하도록 코드를 추가했습니다. 그러나 서버가 로그 아웃하지 않으면 타이머가 계속 실행되므로 타이머가 두 번 실행되기 시작합니다. 타이머는 로그 아웃 한 횟수에 따라 2 또는 그 이상을 뺍니다. 타이머가 이미 실행 중인지 확인하기위한 코드가 있으면 문제가 해결 될 것이라고 생각합니다. 이것을 할 수있는 방법이 있습니까? 도와주세요!