perf[event]: do not init event executors when not use AsyncThread

This commit is contained in:
godotg
2024-04-10 19:02:47 +08:00
parent 070000fbe4
commit ad67c2f5ec
3 changed files with 12 additions and 10 deletions
@@ -15,6 +15,7 @@ package com.zfoo.event.manager;
import com.zfoo.event.enhance.IEventReceiver;
import com.zfoo.event.model.IEvent;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
import com.zfoo.protocol.util.RandomUtils;
import com.zfoo.protocol.util.ThreadUtils;
import org.slf4j.Logger;
@@ -24,6 +25,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@@ -107,6 +110,12 @@ public abstract class EventBus {
receiverMap.computeIfAbsent(eventType, it -> new ArrayList<>(1)).add(receiver);
}
// ------------------------------------------------------------------------------------------------------------------
static final CopyOnWriteHashMapLongObject<ExecutorService> threadMap = new CopyOnWriteHashMapLongObject<>();
public static Executor threadExecutor(long currentThreadId) {
return threadMap.getPrimitive(currentThreadId);
}
}
@@ -1,6 +1,5 @@
package com.zfoo.event.manager;
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.protocol.util.ThreadUtils;
@@ -8,7 +7,6 @@ import io.netty.util.concurrent.FastThreadLocalThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@@ -29,8 +27,6 @@ public abstract class EventExecutors {
private static final ExecutorService[] executors = new ExecutorService[EXECUTORS_SIZE];
private static final CopyOnWriteHashMapLongObject<ExecutorService> threadMap = new CopyOnWriteHashMapLongObject<>(EXECUTORS_SIZE);
static {
for (int i = 0; i < executors.length; i++) {
@@ -59,7 +55,7 @@ public abstract class EventExecutors {
thread.setUncaughtExceptionHandler((t, e) -> logger.error(t.toString(), e));
var executor = executors[poolNumber];
AssertionUtils.notNull(executor);
threadMap.put(thread.getId(), executor);
EventBus.threadMap.put(thread.getId(), executor);
return thread;
}
}
@@ -71,7 +67,4 @@ public abstract class EventExecutors {
executors[Math.abs(executorHash % EXECUTORS_SIZE)].execute(ThreadUtils.safeRunnable(runnable));
}
public static Executor threadExecutor(long currentThreadId) {
return threadMap.getPrimitive(currentThreadId);
}
}
@@ -13,7 +13,7 @@
package com.zfoo.net.task;
import com.zfoo.event.manager.EventExecutors;
import com.zfoo.event.manager.EventBus;
import com.zfoo.net.NetContext;
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
import com.zfoo.protocol.util.AssertionUtils;
@@ -126,7 +126,7 @@ public final class TaskBus {
return taskExecutor;
}
var eventExecutor = EventExecutors.threadExecutor(threadId);
var eventExecutor = EventBus.threadExecutor(threadId);
if (eventExecutor != null) {
return eventExecutor;
}