Skip to content

Instantly share code, notes, and snippets.

@schwarzeszeux
Forked from anonymous/gist:10866752
Last active August 29, 2015 13:59
Show Gist options
  • Save schwarzeszeux/10868535 to your computer and use it in GitHub Desktop.
Save schwarzeszeux/10868535 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 BlockingQueue<Long> queue = new ArrayBlockingQueue<Long>();
public DeadManSwitch(MinecraftServer server, Thread serverThread){
this.server = server;
this.serverThread = serverThread;
DeadManSwitch.instance = this;
}
public void setTimer(){
this.queue.add(System.nanoTime());
}
@Override
public void run() {
System.out.printf("Starting Dead Man Switch\n");
while (server.isServerRunning()){
try {
long first = this.queue.poll(1000, TimeUnit.MILLISECONDS);
long second = this.queue.poll(1000, TimeUnit.MILLISECONDS);
if ((second - first) / 1000. / 1000. > 75.){
System.out.printf("==== Main thread is staled ! %.3f ms ====\n", deltaTime / 1000. / 1000.);
}
} catch {
//InterruptedException
DEAD!!
}
}
}
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