From d62a36815efec972e1eede2fed4bb5569ea4d0f8 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Thu, 30 Jun 2016 14:43:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/Yum/Yum.java | 11 ++++++++--- .../pw/yumc/Yum/listeners/ThreadSafetyListener.java | 4 +--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/pw/yumc/Yum/Yum.java b/src/main/java/pw/yumc/Yum/Yum.java index 84f1cce..30cd46c 100644 --- a/src/main/java/pw/yumc/Yum/Yum.java +++ b/src/main/java/pw/yumc/Yum/Yum.java @@ -30,6 +30,8 @@ import pw.yumc.Yum.runnables.MainThreadCheckTask; * @since 2015年8月21日下午5:14:39 */ public class Yum extends JavaPlugin { + public static Thread mainThread = null; + @Override public FileConfiguration getConfig() { return ConfigManager.i().config; @@ -42,6 +44,9 @@ public class Yum extends JavaPlugin { @Override public void onEnable() { + if (Bukkit.isPrimaryThread()) { + mainThread = Thread.currentThread(); + } new YumAPI(); initCommands(); initListeners(); @@ -92,7 +97,7 @@ public class Yum extends JavaPlugin { new PluginNetworkListener(this); PluginKit.scp("§a网络管理系统已启用..."); } - if (ConfigManager.i().isThreadSafe() && Bukkit.isPrimaryThread()) { + if (ConfigManager.i().isThreadSafe()) { new ThreadSafetyListener(this); PluginKit.scp("§a线程管理系统已启用..."); } @@ -103,10 +108,10 @@ public class Yum extends JavaPlugin { */ private void initRunnable() { // 需要在主线程注册任务 - if (ConfigManager.i().isMainThreadCheck() && Bukkit.isPrimaryThread()) { + if (ConfigManager.i().isMainThreadCheck() && mainThread != null) { final Timer task = new Timer(); PluginKit.scp("§aIO管理系统已启用..."); - task.scheduleAtFixedRate(new MainThreadCheckTask(Thread.currentThread()), 0, 3000); + task.scheduleAtFixedRate(new MainThreadCheckTask(mainThread), 0, 3000); } } } diff --git a/src/main/java/pw/yumc/Yum/listeners/ThreadSafetyListener.java b/src/main/java/pw/yumc/Yum/listeners/ThreadSafetyListener.java index b853c49..fe46b29 100644 --- a/src/main/java/pw/yumc/Yum/listeners/ThreadSafetyListener.java +++ b/src/main/java/pw/yumc/Yum/listeners/ThreadSafetyListener.java @@ -27,8 +27,6 @@ import pw.yumc.Yum.Yum; */ public class ThreadSafetyListener implements Listener { - private final Thread mainThread = Thread.currentThread(); - public ThreadSafetyListener(final Yum yum) { Bukkit.getPluginManager().registerEvents(this, yum); } @@ -99,7 +97,7 @@ public class ThreadSafetyListener implements Listener { } 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(); throw new IllegalAccessError("[Yum 线程安全]: 请勿异步调用一个同步事件 " + eventName); }