cn.hippo4j.common.executor.ThreadPoolManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hippo4j-common Show documentation
Show all versions of hippo4j-common Show documentation
HIPPO4J、HIPPO4J-CORE 公共代码库.
package cn.hippo4j.common.executor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Thread Pool Manager.
*
* @author chen.ma
* @date 2021/6/23 18:36
*/
public class ThreadPoolManager {
private Map>> resourcesManager;
private Map lockers = new ConcurrentHashMap(8);
private static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
private static final AtomicBoolean CLOSED = new AtomicBoolean(false);
public static ThreadPoolManager getInstance() {
return INSTANCE;
}
static {
INSTANCE.init();
}
private void init() {
resourcesManager = new ConcurrentHashMap(8);
}
public void register(String tenantId, String group, ExecutorService executor) {
if (!resourcesManager.containsKey(tenantId)) {
synchronized (this) {
lockers.put(tenantId, new Object());
}
}
final Object monitor = lockers.get(tenantId);
synchronized (monitor) {
Map> map = resourcesManager.get(tenantId);
if (map == null) {
map = new HashMap(8);
map.put(group, new HashSet());
map.get(group).add(executor);
resourcesManager.put(tenantId, map);
return;
}
if (!map.containsKey(group)) {
map.put(group, new HashSet());
}
map.get(group).add(executor);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy