perf[thread]: 优化当前线程的线程组

This commit is contained in:
godotg
2022-09-08 10:55:07 +08:00
parent 5c268e8dcf
commit fed22442e7
5 changed files with 15 additions and 8 deletions
@@ -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;
}
@@ -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";
}
@@ -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;
}
@@ -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;
}
@@ -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()) {