-
-
Save schwarzeszeux/10868535 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 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