Skip to content

Commit

Permalink
perf[event]: do not init event executors when not use AsyncThread
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Apr 10, 2024
1 parent 070000f commit ad67c2f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
9 changes: 9 additions & 0 deletions event/src/main/java/com/zfoo/event/manager/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand All @@ -24,6 +25,8 @@
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;

Expand Down Expand Up @@ -107,6 +110,12 @@ public static void registerEventReceiver(Class<? extends IEvent> eventType, IEve
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);
}
}


Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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;
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;
Expand All @@ -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++) {
Expand Down Expand Up @@ -59,7 +55,7 @@ public Thread newThread(Runnable runnable) {
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;
}
}
Expand All @@ -71,7 +67,4 @@ public static void execute(int executorHash, Runnable runnable) {
executors[Math.abs(executorHash % EXECUTORS_SIZE)].execute(ThreadUtils.safeRunnable(runnable));
}

public static Executor threadExecutor(long currentThreadId) {
return threadMap.getPrimitive(currentThreadId);
}
}
4 changes: 2 additions & 2 deletions net/src/main/java/com/zfoo/net/task/TaskBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -126,7 +126,7 @@ public static Executor currentThreadExecutor() {
return taskExecutor;
}

var eventExecutor = EventExecutors.threadExecutor(threadId);
var eventExecutor = EventBus.threadExecutor(threadId);
if (eventExecutor != null) {
return eventExecutor;
}
Expand Down

0 comments on commit ad67c2f

Please sign in to comment.