mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-15 08:23:41 +00:00
perf[executor]: add method to get executor based on hash
This commit is contained in:
@@ -29,13 +29,11 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -142,8 +140,12 @@ public abstract class EventBus {
|
||||
/**
|
||||
* Use the event thread specified by the hashcode to execute the task
|
||||
*/
|
||||
public static void asyncExecute(int executorHash, Runnable runnable) {
|
||||
executors[Math.abs(executorHash % EXECUTORS_SIZE)].execute(ThreadUtils.safeRunnable(runnable));
|
||||
public static void asyncExecute(int hash, Runnable runnable) {
|
||||
executorOf(hash).execute(ThreadUtils.safeRunnable(runnable));
|
||||
}
|
||||
|
||||
public static ExecutorService executorOf(int hash){
|
||||
return executors[Math.abs(hash % EXECUTORS_SIZE)];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,10 +155,6 @@ public abstract class EventBus {
|
||||
receiverMap.computeIfAbsent(eventType, it -> new ArrayList<>(1)).add(receiver);
|
||||
}
|
||||
|
||||
public static ExecutorService getExecutor(int executorHash){
|
||||
return executors[Math.abs(executorHash % EXECUTORS_SIZE)];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -106,25 +106,25 @@ public final class TaskBus {
|
||||
}
|
||||
|
||||
public static void execute(int taskExecutorHash, Runnable runnable) {
|
||||
executors[calTaskExecutorIndex(taskExecutorHash)].execute(ThreadUtils.safeRunnable(runnable));
|
||||
executorOf(taskExecutorHash).execute(ThreadUtils.safeRunnable(runnable));
|
||||
}
|
||||
|
||||
public static void execute(Object argument, Runnable runnable) {
|
||||
execute(calTaskExecutorHash(argument), runnable);
|
||||
}
|
||||
|
||||
public static ExecutorService executorOf(int hash){
|
||||
return executors[calTaskExecutorIndex(hash)];
|
||||
}
|
||||
|
||||
// 在task,event,scheduler线程执行的异步请求,请求成功过后依然在相同的线程执行回调任务
|
||||
public static Executor currentThreadExecutor() {
|
||||
var threadId = Thread.currentThread().getId();
|
||||
var executor = ThreadUtils.executorByThreadId(threadId);
|
||||
if (executor == null) {
|
||||
return executors[calTaskExecutorIndex(RandomUtils.randomInt())];
|
||||
return executorOf(RandomUtils.randomInt());
|
||||
}
|
||||
return executor;
|
||||
}
|
||||
|
||||
public static ExecutorService getExecutor(int hash){
|
||||
return executors[calTaskExecutorIndex(hash)];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user