io.github.linmoure.thread.ThreadPoolManager Maven / Gradle / Ivy
package io.github.linmoure.thread;
import io.github.linmoure.thread.config.ThreadConfig;
import io.github.linmoure.thread.factory.NameExecutorFactory;
import io.github.linmoure.thread.schedule.ThreadSchedule;
import io.github.linmoure.thread.utils.FileUtils;
import io.github.linmoure.thread.utils.JsonUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;
public final class ThreadPoolManager {
private static final String CONFIG_FILE = "thread-pool-config.json";
private static final Map POOLS;
private static final NameExecutorFactory EXECUTOR_FACTORY;
private static final ThreadSchedule THREAD_SCHEDULE;
private static final String DEFAULT_NS;
static {
String configJson = FileUtils.readConfig(CONFIG_FILE);
ThreadConfig config = (ThreadConfig) JsonUtils.fromJson(configJson, ThreadConfig.class);
DEFAULT_NS = config.getDefaultNameSpace();
EXECUTOR_FACTORY = new NameExecutorFactory(config.getPoolConfigs());
THREAD_SCHEDULE = new ThreadSchedule(config.getSchedulePoolConfig());
POOLS = new ConcurrentHashMap<>(config.getPoolConfigs().size());
}
public static ThreadPoolExecutor getExecutor(String namespace) {
return POOLS.computeIfAbsent(namespace, (s) -> {
return EXECUTOR_FACTORY.get(namespace);
});
}
public static ThreadPoolExecutor getDefaultExecutor() {
return POOLS.computeIfAbsent(DEFAULT_NS, (s) -> {
return EXECUTOR_FACTORY.get(DEFAULT_NS);
});
}
}