perf[orm]: find thread by threadExecutorMap

This commit is contained in:
jaysunxiao
2024-09-23 19:34:48 +08:00
parent 6dd5b3ac65
commit 082c2fa581
6 changed files with 37 additions and 31 deletions
@@ -13,6 +13,7 @@
package com.zfoo.protocol.util;
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
import com.zfoo.protocol.model.Pair;
import io.netty.util.concurrent.EventExecutorGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -40,25 +41,6 @@ public abstract class ThreadUtils {
}
}
/**
* 通过线程号寻找对应的线程
*/
public static Thread findThread(long threadId) {
var group = Thread.currentThread().getThreadGroup();
while (group != null) {
var threads = new Thread[group.activeCount() * 2];
var count = group.enumerate(threads, true);
for (var i = 0; i < count; i++) {
if (threadId == threads[i].getId()) {
return threads[i];
}
}
group = group.getParent();
}
return null;
}
public static void shutdown(ExecutorService executor) {
try {
if (!executor.isTerminated()) {
@@ -127,14 +109,38 @@ public abstract class ThreadUtils {
}
// -----------------------------------------------------------------------------------------------------------------
// threadId -> Executor
private static final CopyOnWriteHashMapLongObject<Executor> threadExecutorMap = new CopyOnWriteHashMapLongObject<>(Runtime.getRuntime().availableProcessors() * 8);
public static void registerExecutor(long threadId, Executor executor) {
threadExecutorMap.put(threadId, executor);
// threadId -> (Thread, Executor)
private static final CopyOnWriteHashMapLongObject<Pair<Thread, Executor>> threadExecutorMap = new CopyOnWriteHashMapLongObject<>(Runtime.getRuntime().availableProcessors() * 8);
public static void registerSingleThreadExecutor(Thread thread, Executor executor) {
threadExecutorMap.put(thread.getId(), new Pair<>(thread, executor));
}
public static Executor executorByThreadId(long threadId) {
return threadExecutorMap.get(threadId);
var threadExecutor = threadExecutorMap.getPrimitive(threadId);
return threadExecutor == null ? null : threadExecutor.getValue();
}
/**
* search for the corresponding thread by the thread id
*/
public static Thread findThread(long threadId) {
var threadExecutor = threadExecutorMap.getPrimitive(threadId);
if (threadExecutor != null) {
return threadExecutor.getKey();
}
var group = Thread.currentThread().getThreadGroup();
while (group != null) {
var threads = new Thread[group.activeCount() * 2];
var count = group.enumerate(threads, true);
for (var i = 0; i < count; i++) {
if (threadId == threads[i].getId()) {
return threads[i];
}
}
group = group.getParent();
}
return null;
}
}