Skip to content

Instantly share code, notes, and snippets.

Created April 16, 2014 12:33
Show Gist options
  • Save anonymous/10866752 to your computer and use it in GitHub Desktop.
Save anonymous/10866752 to your computer and use it in GitHub Desktop.
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