3

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:

  1. Is it the correct way of using a single ThreadpoolExecutor throughout the app?
  2. How can I execute most priority task first if the pool queue has number of tasks to be executed?
  3. If I use two or more ThreadPoolExecutors then Can I give more priority to one of the ThreadPoolExecutor?

Thanks in advance.

pradeep_ch
  • 124
  • 8
  • I'm not sure if you automatically want to create a new pool when the existing one is shut down. Presumably, the application is terminating at that point, and no more tasks should be accepted. – Thilo Apr 09 '15 at 10:09
  • Task prioritization ... http://stackoverflow.com/questions/807223/how-do-i-implement-task-prioritization-using-an-executorservice-in-java-5 – Amit Yadav Nov 29 '15 at 14:26

0 Answers0