Home > Mobile >  Thread pool in each api request vs single application pool reuse
Thread pool in each api request vs single application pool reuse

Time:01-02

I want to run few task in parallel on each request I receive from client . I am using spring boot to run server and on HTTP api call it calls getItems method . I am using Executors.newCachedThreadPool() to spawn multiple threads . I need few inputs on below implementation

  1. Is it good to have ExecutorService thread pool with each request or create a pool once for application and reuse it?
  2. How to decide the pool size for the Executors service.
@Override
            public List<Item> getItems(List<String> id) {
                List<Item> items = new ArrayList<>();
                ExecutorService execSvc = Executors.newCachedThreadPool();
                List<Callable<Item> > inventories = id.stream()
                        .map((itemId)-> (Callable<Item>) () -> {
                            List<InventoryNode> inventoryNodes =  getInventory(itemId);
                            return getItem(inventoryNodes);
                        }).collect(Collectors.toList());
                try {
                    List<Future<Item>> results = execSvc.invokeAll(inventories);
                    for(Future<Item> inventory :results){
                        if(inventory.isDone()){
                            items.add(inventory.get());
                        }
                    }
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }finally {
                    execSvc.shutdown();
                }
                return items;
            }

CodePudding user response:

A thread is an independent path of execution through program code. Creating a thread is an expensive task. There is a lot of work needs to be done like allocating memory, initializing the thread stack.

  1. It is good to create thread pool once for the application and not on each request. It reduces overhead and latency in processing the request. In Spring boot you can define a bean for this.

     @Bean("cachedThreadPool")
     public ExecutorService cachedThreadPool() {
        return Executors.newCachedThreadPool();
     }
    
  2. Consider a database connection with a maximum pool size of 50. It doesn't make sense to have a thread pool of 100 in this case. So it is always better to decide on maximum numbers of threads based on the use case you're on.

  • Related