fix: 修复线程检查任务

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-06-30 14:43:17 +08:00
parent b3dea9a5e3
commit d62a36815e
2 changed files with 9 additions and 6 deletions

View File

@ -30,6 +30,8 @@ import pw.yumc.Yum.runnables.MainThreadCheckTask;
* @since 2015年8月21日下午5:14:39 * @since 2015年8月21日下午5:14:39
*/ */
public class Yum extends JavaPlugin { public class Yum extends JavaPlugin {
public static Thread mainThread = null;
@Override @Override
public FileConfiguration getConfig() { public FileConfiguration getConfig() {
return ConfigManager.i().config; return ConfigManager.i().config;
@ -42,6 +44,9 @@ public class Yum extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
if (Bukkit.isPrimaryThread()) {
mainThread = Thread.currentThread();
}
new YumAPI(); new YumAPI();
initCommands(); initCommands();
initListeners(); initListeners();
@ -92,7 +97,7 @@ public class Yum extends JavaPlugin {
new PluginNetworkListener(this); new PluginNetworkListener(this);
PluginKit.scp("§a网络管理系统已启用..."); PluginKit.scp("§a网络管理系统已启用...");
} }
if (ConfigManager.i().isThreadSafe() && Bukkit.isPrimaryThread()) { if (ConfigManager.i().isThreadSafe()) {
new ThreadSafetyListener(this); new ThreadSafetyListener(this);
PluginKit.scp("§a线程管理系统已启用..."); PluginKit.scp("§a线程管理系统已启用...");
} }
@ -103,10 +108,10 @@ public class Yum extends JavaPlugin {
*/ */
private void initRunnable() { private void initRunnable() {
// 需要在主线程注册任务 // 需要在主线程注册任务
if (ConfigManager.i().isMainThreadCheck() && Bukkit.isPrimaryThread()) { if (ConfigManager.i().isMainThreadCheck() && mainThread != null) {
final Timer task = new Timer(); final Timer task = new Timer();
PluginKit.scp("§aIO管理系统已启用..."); PluginKit.scp("§aIO管理系统已启用...");
task.scheduleAtFixedRate(new MainThreadCheckTask(Thread.currentThread()), 0, 3000); task.scheduleAtFixedRate(new MainThreadCheckTask(mainThread), 0, 3000);
} }
} }
} }

View File

@ -27,8 +27,6 @@ import pw.yumc.Yum.Yum;
*/ */
public class ThreadSafetyListener implements Listener { public class ThreadSafetyListener implements Listener {
private final Thread mainThread = Thread.currentThread();
public ThreadSafetyListener(final Yum yum) { public ThreadSafetyListener(final Yum yum) {
Bukkit.getPluginManager().registerEvents(this, yum); Bukkit.getPluginManager().registerEvents(this, yum);
} }
@ -99,7 +97,7 @@ public class ThreadSafetyListener implements Listener {
} }
private void checkSafety(final Event eventType) { private void checkSafety(final Event eventType) {
if (Thread.currentThread() != mainThread && !eventType.isAsynchronous()) { if (Yum.mainThread != null && Thread.currentThread() != Yum.mainThread && !eventType.isAsynchronous()) {
final String eventName = eventType.getEventName(); final String eventName = eventType.getEventName();
throw new IllegalAccessError("[Yum 线程安全]: 请勿异步调用一个同步事件 " + eventName); throw new IllegalAccessError("[Yum 线程安全]: 请勿异步调用一个同步事件 " + eventName);
} }