java - HashMap for enum as key -
i had posted similar question before also. got clarification doubts well. still need more. hashmap initialized enum object key , threadpool instance value. confused of how initialize hashmap every object been called other process ..to make clear : program, mythreadpoolexcecutorpgm.java initializes hashmap progran additionhandler.java requests thread hashmap passing threadpoolname (enum). getting "no thread available hashmap" message. please me.
below given 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>(); (threadpoolname poolname : threadpoolname.) // failing 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 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 using enum tpe = mythreadpoolexcecutorpgm.getthreadpoolexcecutor(poolname); tpe.execute(setobj); } public static void main(string[] args) { additionhandler obj = new additionhandler(); obj.handle(); } }
i suspect you're looking static values()
method added every enum:
for (threadpoolname poolname : threadpoolname.getvalues())
alternatively, can use enumset.allof()
:
for (threadpoolname poolname : enumset.allof(threadpoolname.class))
(as bozho says, enummap
alternative here. still need loop through enum values.)
Comments
Post a Comment