Created
April 16, 2014 12:33
-
-
Save anonymous/10866752 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DeadManSwitch implements Runnable{ | |
public static DeadManSwitch instance; | |
private Thread serverThread = null; | |
private MinecraftServer server = null; | |
private Long timer = 0L; | |
public DeadManSwitch(MinecraftServer server, Thread serverThread){ | |
this.server = server; | |
this.serverThread = serverThread; | |
DeadManSwitch.instance = this; | |
} | |
public synchronized void setTimer(){ | |
this.timer = System.nanoTime(); | |
} | |
public synchronized long getTimer(){ | |
return this.timer; | |
} | |
@Override | |
public void run() { | |
System.out.printf("Starting Dead Man Switch\n"); | |
while (server.isServerRunning()){ | |
try{ | |
long deltaTime = System.nanoTime() - this.getTimer(); | |
if (deltaTime / 1000. / 1000. > 75.){ | |
System.out.printf("==== Main thread is staled ! %.3f ms ====\n", deltaTime / 1000. / 1000.); | |
} | |
Thread.sleep(1L); | |
} catch (InterruptedException e){ | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
public static DeadManSwitch startDeadManSwitch(MinecraftServer server){ | |
DeadManSwitch deadManSwitch = new DeadManSwitch(server, Thread.currentThread()); | |
Thread deadManSwitchThrd = new Thread(deadManSwitch); | |
deadManSwitchThrd.setName("Dead Man Switch"); | |
deadManSwitchThrd.setPriority(Thread.MAX_PRIORITY); | |
//deadManSwitchThrd.setDaemon(false); | |
deadManSwitchThrd.start(); | |
return deadManSwitch; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment