I have created a ThreadPoolExecutor like this.
public class MYThreadPoolExecutor {
private int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
private ThreadPoolExecutor executor;
private static MyThreadPoolExecutor mInstance;
private KoreThreadPoolExecutor(){
}
public static synchronized MyThreadPoolExecutor getInstance(){
if(mInstance == null)
mInstance = new MyThreadPoolExecutor();
return mInstance;
}
public ThreadPoolExecutor getExecutor(){
if (executor == null || executor.isShutdown()) {
executor = new ThreadPoolExecutor(NUMBER_OF_CORES*3,
NUMBER_OF_CORES*3,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
}
return executor;
}
}
And now I am using this class through out the app for db operations and etc.. like this
MyThreadPoolExecutor.getInstance().getExecutor().execute(new Runnable() {
@Override
public void run() {
//DB operation
}
});
Now I have some db operations running in this pool, and if I get any notification then I have to execute that notification related things first.
Now my questions are:
- Is it the correct way of using a single ThreadpoolExecutor throughout the app?
- How can I execute most priority task first if the pool queue has number of tasks to be executed?
- If I use two or more ThreadPoolExecutors then Can I give more priority to one of the ThreadPoolExecutor?
Thanks in advance.