我做了一个甜蜜的系统更新功能 这个游戏我在这里做的是代码:
public static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private static CountDownThread countDownThread;
public static boolean running = false;
private static short updateSeconds;
public static void start() {
System.out.println("starting");
running = true;
countDownThread = new CountDownThread();
scheduler.scheduleWithFixedDelay(countDownThread, 0, 1000, TimeUnit.MILLISECONDS);
时 时
public static void stop() {
System.out.println("Stoping");
scheduler.shutdown();
running = false;
updateSeconds = 0;
System.out.println("Stopped");
时 时
public static void refresh() {
for (Player p : Static.world.players){
if (p.ready()) {
if (updateSeconds > 0) {
ActionSender.sendSystemUpdate(p, updateSeconds+1);
时 时 else {
ActionSender.sendSystemUpdate(p, updateSeconds);
时 时
时 时
时 时
时 时
public static short getUpdateSeconds() {
return updateSeconds;
时 时
public static void setUpdateSeconds(short updateSeconds) {
SystemUpdateHandler.updateSeconds = (short) (updateSeconds);
时 时
public static class CountDownThread implements Runnable {
@Override
public void run() {
System.out.println(updateSeconds);
updateSeconds--;
if (updateSeconds <= 0) {
Static.server.restart();
scheduler.shutdown();
running = false;
时 时
时 时
时 时
时 时
s 这样, 当系统更新计数器达到0时, 服务器将重新启动自己。 它的工作很好, 但问题从这里开始
case "update":
if (Short.parseShort(txtSystemUpdate.getText()) != 0) {
SystemUpdateHandler.setUpdateSeconds(Short.parseShort(txtSystemUpdate.getText()));
SystemUpdateHandler.refresh();
if (!SystemUpdateHandler.running) {
SystemUpdateHandler.start();
时 时
时 时 else {
SystemUpdateHandler.stop();
for (Player p : Static.world.players){
if (p.ready()) {
ActionSender.sendSystemUpdate(p, 0);
时 时
时 时
时 时
break;
这就是我称之为它的地方, 基本上, 如果我输入一个数字高于 0, 程序就运作正常。 但是如果输入数字 0, 调度器将停止运行( 保存内存), 因为不需要它, 除非我发送系统更新 。 基本上, 我输入 0 时如何阻止调度器运行, 但当我输入一个数字 & gt; 然后 0( 几次) 时, 我能够重新启动它 。