java – 将枚举值插入HashMap

我正在做一个程序,我需要将枚举值插入HashMap.我们真的能这样做吗?我在很多方面尝试过它,但失败了.

谁能帮帮我吗?通过该程序,我需要实现一个包含4个线程池(其名称作为键)的HashMap,对应于我有一个ThreapoolExcecutor对象.

下面给出的是我的代码:

public class MyThreadpoolExcecutorPgm {
    enum ThreadpoolName
    {
        DR,
        PQ,
        EVENT,
        MISCELLENEOUS;
    }
    private static String threadName;
    private static HashMap<String, ThreadPoolExecutor> threadpoolExecutorHash;

    public MyThreadpoolExcecutorPgm(String p_threadName) {
        threadName = p_threadName;

    }

    public static void fillthreadpoolExecutorHash() {
        int poolsize = 3;
        int maxpoolsize = 3;
        long keepAliveTime = 10;
        ThreadPoolExecutor tp = null;
        threadpoolExecutorHash = new HashMap<String, ThreadPoolExecutor>();

        ThreadpoolName poolName ;
        tp = new ThreadPoolExecutor(poolsize, maxpoolsize, keepAliveTime,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));

        threadpoolExecutorHash.put(poolName,tp);    //Here i am failing to implement currect put()


    }
最佳答案
您可能需要考虑在此处使用EnumMap而不是HashMap.使用枚举值时,EnumMap比HashMap更快,更节省空间,这看起来正是您在这里所做的.

转载注明原文:java – 将枚举值插入HashMap - 代码日志