Execute multiple queries in parallel via Streams

后端 未结 4 1539
日久生厌
日久生厌 2020-12-14 05:00

I am having the following method:

public String getResult() {

        List serversList = getServerListFromDB();

        List ap         


        
4条回答
  •  被撕碎了的回忆
    2020-12-14 05:28

    As already mentioned, a standard parallel stream is probably not the best fit for your use case. I would complete each task asynchronously using an ExecutorService and "join" them when calling the getResult method:

    ExecutorService es = Executors.newFixedThreadPool(3);
    
    Future> serversList = es.submit(() -> getServerListFromDB());
    Future> appList = es.submit(() -> getAppListFromDB());
    Future> userList = es.submit(() -> getUserFromDB());
    
    return getResult(serversList.get(), appList.get(), userList.get());
    

提交回复
热议问题