mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-14 02:16:31 +00:00
perf[event]: do not init event executors when not use AsyncThread
This commit is contained in:
1 parent
070000fbe4
commit
ad67c2f5ec
3 files changed
+12
-10
No files matched your search
@@ -15,6 +15,7 @@ package com.zfoo.event.manager;
|
|||||||
import com.zfoo.event.enhance.IEventReceiver;
|
import com.zfoo.event.enhance.IEventReceiver;
|
||||||
import com.zfoo.event.model.IEvent;
|
import com.zfoo.event.model.IEvent;
|
||||||
import com.zfoo.protocol.collection.CollectionUtils;
|
import com.zfoo.protocol.collection.CollectionUtils;
|
||||||
|
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
|
||||||
import com.zfoo.protocol.util.RandomUtils;
|
import com.zfoo.protocol.util.RandomUtils;
|
||||||
import com.zfoo.protocol.util.ThreadUtils;
|
import com.zfoo.protocol.util.ThreadUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -24,6 +25,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@@ -107,6 +110,12 @@ public abstract class EventBus {
|
|||||||
receiverMap.computeIfAbsent(eventType, it -> new ArrayList<>(1)).add(receiver);
|
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;
|
package com.zfoo.event.manager;
|
||||||
|
|
||||||
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
|
|
||||||
import com.zfoo.protocol.util.AssertionUtils;
|
import com.zfoo.protocol.util.AssertionUtils;
|
||||||
import com.zfoo.protocol.util.StringUtils;
|
import com.zfoo.protocol.util.StringUtils;
|
||||||
import com.zfoo.protocol.util.ThreadUtils;
|
import com.zfoo.protocol.util.ThreadUtils;
|
||||||
@@ -8,7 +7,6 @@ import io.netty.util.concurrent.FastThreadLocalThread;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ThreadFactory;
|
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 ExecutorService[] executors = new ExecutorService[EXECUTORS_SIZE];
|
||||||
|
|
||||||
private static final CopyOnWriteHashMapLongObject<ExecutorService> threadMap = new CopyOnWriteHashMapLongObject<>(EXECUTORS_SIZE);
|
|
||||||
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
for (int i = 0; i < executors.length; i++) {
|
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));
|
thread.setUncaughtExceptionHandler((t, e) -> logger.error(t.toString(), e));
|
||||||
var executor = executors[poolNumber];
|
var executor = executors[poolNumber];
|
||||||
AssertionUtils.notNull(executor);
|
AssertionUtils.notNull(executor);
|
||||||
threadMap.put(thread.getId(), executor);
|
EventBus.threadMap.put(thread.getId(), executor);
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,7 +67,4 @@ public abstract class EventExecutors {
|
|||||||
executors[Math.abs(executorHash % EXECUTORS_SIZE)].execute(ThreadUtils.safeRunnable(runnable));
|
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;
|
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.net.NetContext;
|
||||||
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
|
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
|
||||||
import com.zfoo.protocol.util.AssertionUtils;
|
import com.zfoo.protocol.util.AssertionUtils;
|
||||||
@@ -126,7 +126,7 @@ public final class TaskBus {
|
|||||||
return taskExecutor;
|
return taskExecutor;
|
||||||
}
|
}
|
||||||
|
|
||||||
var eventExecutor = EventExecutors.threadExecutor(threadId);
|
var eventExecutor = EventBus.threadExecutor(threadId);
|
||||||
if (eventExecutor != null) {
|
if (eventExecutor != null) {
|
||||||
return eventExecutor;
|
return eventExecutor;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user