feat: 添加心跳检测 防止主线程被关闭

Signed-off-by: 502647092 <admin@yumc.pw>
dev
502647092 2016-07-05 16:50:17 +08:00
parent 785295c65c
commit a236157ec3
2 changed files with 25 additions and 8 deletions

View File

@ -4,6 +4,7 @@
package pw.yumc.Yum;
import java.util.Timer;
import java.util.TimerTask;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
@ -31,7 +32,8 @@ import pw.yumc.Yum.runnables.MainThreadCheckTask;
*/
public class Yum extends JavaPlugin {
public static Thread mainThread = null;
public static final Timer task = new Timer();
public static Timer task = new Timer();
public static TimerTask tt;
@Override
public FileConfiguration getConfig() {
@ -41,7 +43,7 @@ public class Yum extends JavaPlugin {
@Override
public void onDisable() {
NetworkManager.unregister();
task.cancel();
tt.cancel();
}
@Override
@ -112,7 +114,7 @@ public class Yum extends JavaPlugin {
// 需要在主线程注册任务
if (ConfigManager.i().isMainThreadCheck() && mainThread != null) {
PluginKit.scp("§aIO管理系统已启用...");
task.scheduleAtFixedRate(new MainThreadCheckTask(mainThread), 0, 3000);
task.scheduleAtFixedRate(tt = new MainThreadCheckTask(mainThread), 0, 5000);
}
}
}

View File

@ -5,6 +5,7 @@ import java.util.TimerTask;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.kit.PKit;
import cn.citycraft.PluginHelper.kit.PluginKit;
/**
@ -14,11 +15,13 @@ import cn.citycraft.PluginHelper.kit.PluginKit;
* @author
*/
public class MainThreadCheckTask extends TimerTask {
public String prefix = "§6[§bYum §a线程管理§6] ";
public String warnPNet = "§6插件 §b%s §c在主线程进行网络操作 §4服务器处于停止状态...";
public String warnPIO = "§6插件 §b%s §c在主线程进行IO操作 §4服务器处于停止状态...";
public String warnNet = "§c主线程存在网络操作 §4服务器处于停止状态...";
public String warnIO = "§c主线程存在IO操作 §4服务器处于停止状态...";
private final String prefix = "§6[§bYum §a线程管理§6] ";
private final String warnPNet = "§6插件 §b%s §c在主线程进行网络操作 §4服务器处于停止状态...";
private final String warnPIO = "§6插件 §b%s §c在主线程进行IO操作 §4服务器处于停止状态...";
private final String warnNet = "§c主线程存在网络操作 §4服务器处于停止状态...";
private final String warnIO = "§c主线程存在IO操作 §4服务器处于停止状态...";
private final String deliver = "§c服务器处于停止状态 已超过 %s 秒 激活心跳 防止线程关闭...";
private int stopTime = 0;
private final Thread mainThread;
public MainThreadCheckTask(final Thread mainThread) {
@ -48,6 +51,7 @@ public class MainThreadCheckTask extends TimerTask {
} else {
PluginKit.sc(prefix + warnNet);
}
tick();
}
// File (in) - java.io.FileInputStream.readBytes
// File (out) - java.io.FileOutputStream.writeBytes
@ -58,6 +62,9 @@ public class MainThreadCheckTask extends TimerTask {
} else {
PluginKit.sc(prefix + warnIO);
}
tick();
} else {
stopTime = 0;
}
}
}
@ -66,4 +73,12 @@ public class MainThreadCheckTask extends TimerTask {
private boolean isElementEqual(final StackTraceElement traceElement, final String className, final String methodName) {
return traceElement.getClassName().equals(className) && traceElement.getMethodName().equals(methodName);
}
private void tick() {
stopTime += 5;
if (stopTime >= 45) {
PluginKit.sc(String.format(prefix + deliver, stopTime));
PKit.tick();
}
}
}