I want to initialize Synchronized static singleton ThreadPool Executor with my defined properties.
I want it to available through out the application and should be destroyed when server is restarted or stopped.
public class ThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor {
private static Properties properties;
static {
try {
properties.load(ThreadPoolExecutor .class.getClassLoader().getResourceAsStream("config.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
static final int defaultCorePoolSize = Integer.valueOf((String) properties.get("CORE_POOL_SIZE"));
static final int defaultMaximumPoolSize = Integer.valueOf((String) properties.get("MAX_POOL_SIZE"));
static final long defaultKeepAliveTime = Integer.valueOf((String) properties.get("KEEP_ALIVE_TIME"));
static final TimeUnit defaultTimeUnit = TimeUnit.MINUTES;
static final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
private static ThreadPoolExecutor instance;
private ThreadPoolExecutor() {
super(defaultCorePoolSize, defaultMaximumPoolSize, defaultKeepAliveTime, defaultTimeUnit, workQueue);
}
synchronized static ThreadPoolExecutor getInstance() {
if (instance == null) {
instance = new ThreadPoolExecutor();
}
return instance;
}
This is what I have done till now (I am newbie to Multi-threading).
As my application is completely based on Multi-threading, how do I achieve my requirements and anything to improve here! As I said, how do i maintain/make it available through out the application.