All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.hippo4j.common.executor.ThreadPoolManager Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
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