From fed22442e78e432d15602030b277d210f808fe4c Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 8 Sep 2022 10:55:07 +0800 Subject: [PATCH] =?UTF-8?q?perf[thread]:=20=E4=BC=98=E5=8C=96=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E7=BA=BF=E7=A8=8B=E7=9A=84=E7=BA=BF=E7=A8=8B=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- event/src/main/java/com/zfoo/event/manager/EventBus.java | 4 ++-- .../com/zfoo/net/consumer/registry/ZookeeperRegistry.java | 3 +-- net/src/main/java/com/zfoo/net/task/TaskBus.java | 4 ++-- .../java/com/zfoo/scheduler/manager/SchedulerBus.java | 4 ++-- util/src/main/java/com/zfoo/util/ThreadUtils.java | 8 ++++++++ 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/event/src/main/java/com/zfoo/event/manager/EventBus.java b/event/src/main/java/com/zfoo/event/manager/EventBus.java index 5c9934e4..dcb79d38 100644 --- a/event/src/main/java/com/zfoo/event/manager/EventBus.java +++ b/event/src/main/java/com/zfoo/event/manager/EventBus.java @@ -19,6 +19,7 @@ import com.zfoo.protocol.collection.CollectionUtils; import com.zfoo.protocol.util.AssertionUtils; import com.zfoo.protocol.util.StringUtils; import com.zfoo.util.SafeRunnable; +import com.zfoo.util.ThreadUtils; import com.zfoo.util.math.RandomUtils; import io.netty.util.concurrent.FastThreadLocalThread; import org.slf4j.Logger; @@ -64,8 +65,7 @@ public abstract class EventBus { private final ThreadGroup group; public EventThreadFactory(int poolNumber) { - var s = System.getSecurityManager(); - group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + this.group = ThreadUtils.currentThreadGroup(); this.poolNumber = poolNumber; } diff --git a/net/src/main/java/com/zfoo/net/consumer/registry/ZookeeperRegistry.java b/net/src/main/java/com/zfoo/net/consumer/registry/ZookeeperRegistry.java index b6c7e635..901a464a 100644 --- a/net/src/main/java/com/zfoo/net/consumer/registry/ZookeeperRegistry.java +++ b/net/src/main/java/com/zfoo/net/consumer/registry/ZookeeperRegistry.java @@ -88,8 +88,7 @@ public class ZookeeperRegistry implements IRegistry { // config-p1-t1 = config-pool-1-thread-1 ConfigThreadFactory() { - SecurityManager s = System.getSecurityManager(); - group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + this.group = ThreadUtils.currentThreadGroup(); namePrefix = "config-p" + poolNumber.getAndIncrement() + "-t"; } diff --git a/net/src/main/java/com/zfoo/net/task/TaskBus.java b/net/src/main/java/com/zfoo/net/task/TaskBus.java index 6c01d569..99301a5e 100644 --- a/net/src/main/java/com/zfoo/net/task/TaskBus.java +++ b/net/src/main/java/com/zfoo/net/task/TaskBus.java @@ -22,6 +22,7 @@ import com.zfoo.protocol.util.AssertionUtils; import com.zfoo.protocol.util.StringUtils; import com.zfoo.scheduler.manager.SchedulerBus; import com.zfoo.util.SafeRunnable; +import com.zfoo.util.ThreadUtils; import com.zfoo.util.math.RandomUtils; import io.netty.util.concurrent.FastThreadLocalThread; import org.slf4j.Logger; @@ -78,8 +79,7 @@ public final class TaskBus { private final ThreadGroup group; public TaskThreadFactory(int poolNumber) { - var s = System.getSecurityManager(); - group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + this.group = ThreadUtils.currentThreadGroup(); this.poolNumber = poolNumber; } diff --git a/scheduler/src/main/java/com/zfoo/scheduler/manager/SchedulerBus.java b/scheduler/src/main/java/com/zfoo/scheduler/manager/SchedulerBus.java index 9d7060a7..8d9d7480 100644 --- a/scheduler/src/main/java/com/zfoo/scheduler/manager/SchedulerBus.java +++ b/scheduler/src/main/java/com/zfoo/scheduler/manager/SchedulerBus.java @@ -19,6 +19,7 @@ import com.zfoo.scheduler.SchedulerContext; import com.zfoo.scheduler.model.vo.SchedulerDefinition; import com.zfoo.scheduler.util.TimeUtils; import com.zfoo.util.SafeRunnable; +import com.zfoo.util.ThreadUtils; import io.netty.util.concurrent.FastThreadLocalThread; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,8 +78,7 @@ public abstract class SchedulerBus { private final ThreadGroup group; public SchedulerThreadFactory(int poolNumber) { - var s = System.getSecurityManager(); - group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + this.group = ThreadUtils.currentThreadGroup(); this.poolNumber = poolNumber; } diff --git a/util/src/main/java/com/zfoo/util/ThreadUtils.java b/util/src/main/java/com/zfoo/util/ThreadUtils.java index af6c78ba..731ee5ab 100644 --- a/util/src/main/java/com/zfoo/util/ThreadUtils.java +++ b/util/src/main/java/com/zfoo/util/ThreadUtils.java @@ -40,6 +40,14 @@ public abstract class ThreadUtils { } } + /** + * 获取当前线程的线程组 + */ + public static ThreadGroup currentThreadGroup() { + var s = System.getSecurityManager(); + return (null != s) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + } + public static void shutdown(ExecutorService executor) { try { if (!executor.isTerminated()) {