简体   繁体   中英

HashMap for enum as key

I had posted somewhat similar question before also. I got clarification for my doubts as well. But still I need something more. The Hashmap will be initialized with the enum object as the key and a threadpool instance as the value. I am confused as of how to initialize the HashMap for every object been called by some other process ..To make clear : My program, MyThreadpoolExcecutorPgm.java initializes a HashMap My Progran AdditionHandler.java requests a thread from the HashMap by passing ThreadpoolName (enum). I am getting "No thread available from HashMap" message. Please do help me.
Below given is my code:

 public class MyThreadpoolExcecutorPgm {

    enum ThreadpoolName {
        DR, BR, SV, MISCELLENEOUS;
    }

    private static String threadName;
    private static HashMap<ThreadpoolName, 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<ThreadpoolName, ThreadPoolExecutor>();
        for (ThreadpoolName poolName : ThreadpoolName.) // failing to implement
        {
            tp = new ThreadPoolExecutor(poolsize, maxpoolsize, keepAliveTime,
                    TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
            threadpoolExecutorHash.put(poolName, tp);
        }
    }

    public static ThreadPoolExecutor getThreadpoolExcecutor(
            ThreadpoolName poolName) {
        ThreadPoolExecutor thread = null;
        if (threadpoolExecutorHash != null && poolName != null) {
            thread = threadpoolExecutorHash.get(poolName);
        } else {
            System.out.println("No thread available from HashMap");
        }
        return thread;
    }
}

AdditionHandler.java

public class AdditionHandler{

    public void handle() {
        AddProcess setObj = new AddProcess(5, 20);
        ThreadPoolExecutor tpe = null;
        ThreadpoolName poolName =ThreadpoolName.DR; //i am using my enum    
        tpe = MyThreadpoolExcecutorPgm.getThreadpoolExcecutor(poolName);
        tpe.execute(setObj);
    }

    public static void main(String[] args) {
        AdditionHandler obj = new AdditionHandler();
        obj.handle();
    }
}

I suspect you're just looking for the static values() method which is added to every enum:

for (ThreadpoolName poolName : ThreadpoolName.getValues())

Alternatively, you can use EnumSet.allOf() :

for (ThreadpoolName poolName : EnumSet.allOf(ThreadpoolName.class))

(As Bozho says, EnumMap is a good alternative here. You still need to loop through the enum values.)

First, you'd better use EnumMap . Then make sure you have filled the map before you invoked the method.

You can iterate through enum values by one of (in descending order of preference)

for(Enum value : Enum.values())

for(Enum value : EnumSet.allOf(Enum.class))

for(Enum value : Enum.class.getEnumConstants())

But you should also be using an EnumMap .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM