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

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
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; package pw.yumc.Yum;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -31,7 +32,8 @@ import pw.yumc.Yum.runnables.MainThreadCheckTask;
*/ */
public class Yum extends JavaPlugin { public class Yum extends JavaPlugin {
public static Thread mainThread = null; public static Thread mainThread = null;
public static final Timer task = new Timer(); public static Timer task = new Timer();
public static TimerTask tt;
@Override @Override
public FileConfiguration getConfig() { public FileConfiguration getConfig() {
@ -41,7 +43,7 @@ public class Yum extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
NetworkManager.unregister(); NetworkManager.unregister();
task.cancel(); tt.cancel();
} }
@Override @Override
@ -112,7 +114,7 @@ public class Yum extends JavaPlugin {
// 需要在主线程注册任务 // 需要在主线程注册任务
if (ConfigManager.i().isMainThreadCheck() && mainThread != null) { if (ConfigManager.i().isMainThreadCheck() && mainThread != null) {
PluginKit.scp("§aIO管理系统已启用..."); 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 org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.kit.PKit;
import cn.citycraft.PluginHelper.kit.PluginKit; import cn.citycraft.PluginHelper.kit.PluginKit;
/** /**
@ -14,11 +15,13 @@ import cn.citycraft.PluginHelper.kit.PluginKit;
* @author * @author
*/ */
public class MainThreadCheckTask extends TimerTask { public class MainThreadCheckTask extends TimerTask {
public String prefix = "§6[§bYum §a线程管理§6] "; private final String prefix = "§6[§bYum §a线程管理§6] ";
public String warnPNet = "§6插件 §b%s §c在主线程进行网络操作 §4服务器处于停止状态..."; private final String warnPNet = "§6插件 §b%s §c在主线程进行网络操作 §4服务器处于停止状态...";
public String warnPIO = "§6插件 §b%s §c在主线程进行IO操作 §4服务器处于停止状态..."; private final String warnPIO = "§6插件 §b%s §c在主线程进行IO操作 §4服务器处于停止状态...";
public String warnNet = "§c主线程存在网络操作 §4服务器处于停止状态..."; private final String warnNet = "§c主线程存在网络操作 §4服务器处于停止状态...";
public String warnIO = "§c主线程存在IO操作 §4服务器处于停止状态..."; private final String warnIO = "§c主线程存在IO操作 §4服务器处于停止状态...";
private final String deliver = "§c服务器处于停止状态 已超过 %s 秒 激活心跳 防止线程关闭...";
private int stopTime = 0;
private final Thread mainThread; private final Thread mainThread;
public MainThreadCheckTask(final Thread mainThread) { public MainThreadCheckTask(final Thread mainThread) {
@ -48,6 +51,7 @@ public class MainThreadCheckTask extends TimerTask {
} else { } else {
PluginKit.sc(prefix + warnNet); PluginKit.sc(prefix + warnNet);
} }
tick();
} }
// File (in) - java.io.FileInputStream.readBytes // File (in) - java.io.FileInputStream.readBytes
// File (out) - java.io.FileOutputStream.writeBytes // File (out) - java.io.FileOutputStream.writeBytes
@ -58,6 +62,9 @@ public class MainThreadCheckTask extends TimerTask {
} else { } else {
PluginKit.sc(prefix + warnIO); 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) { private boolean isElementEqual(final StackTraceElement traceElement, final String className, final String methodName) {
return traceElement.getClassName().equals(className) && traceElement.getMethodName().equals(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();
}
}
} }