Search code examples
scheduled-tasksexecutorserviceinstances

Running two instances of the ScheduledThreadPoolExecutor


I have a number of asynchronous tasks to run in parallel. All the tasks can be divided into two types, lets call one - type A (that are time consuming) and everything else type B (faster and quick to execute ones). with a single ScheduledThreadPoolExecutor with x poolsize, eventually at some point all threads are busy executing type A, as a resul type B gets blocked and delayed. what im trying to accomplish is to run a type A tasks parallel to type B, and i want tasks in both the types to run parallel within their group for performance .

Would you think its prudent to have two instances of ScheduledThreadPoolExecutor for the type A and B exclusively with their own thread pools ? Do you see any issues with this approach?


Solution

  • No, that's seems reasonable. I am doing something similar i.e. I need to execute tasks in serial fashion depending on some id e.g. all the tasks which are for component with id="1" need to be executed serially to each another and in parallel to all other tasks which are for components with different ids. so basically I need a separate queue of tasks for each different component, the tasks are pulled one after another from each specific queue. In order to achieve that I use

    Executors.newSingleThreadExecutor(new JobThreadFactory(componentId));
    

    for each component. Additionally I need ExecutorService for a different type of tasks which are not bound to componentIds, for that I create additional ExecutorService instance

    Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE, new JobThreadFactory());
    

    This works fine for my case at least. The only problem I can think of if there is a need of ordered execution of the tasks i.e. task2 NEEDS to be executed after task1 and so on... But I doubt this the case here ...