Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class Main {
    public static final int TOTAL_SIZE = 256;
    private static final Map<String, ExecutorService> map = new ConcurrentHashMap<String, ExecutorService>();

    public static int getActiveCount(Class clazz) {
        ThreadPoolExecutor executor = (ThreadPoolExecutor) getExecutorService(clazz);
        return executor.getActiveCount();
    }

    private static ExecutorService getExecutorService(Class clazz) {
        String key = clazz.getName();
        if (!map.containsKey(key)) {
            synchronized (map) {
                if (!map.containsKey(key)) {
                    map.put(key, Executors.newFixedThreadPool(TOTAL_SIZE));
                }
            }
        }
        return map.get(key);
    }
}