Multithreading best practices : constraining tasks newFixedThreadPool

前端 未结 5 1146
执笔经年
执笔经年 2021-01-15 01:31

I want to launch a lot of tasks to run on a database of +-42Mio records. I want to run this in batches of 5000 records/time (results in 850 tasks). I also want to limit the

5条回答
  •  滥情空心
    2021-01-15 02:08

    The best way would be to use countdownlatch as follows

        ExecutorService executorService = Executors.newFixedThreadPool(16);
      CountdownLatch latch = new CountdownLatch(900);
     FetcherRunner runner = new FetcherRunner(routes, start, stop, latch);
     latch.await();
    

    in the FetcherRunner under finally block use latch.countDown(); code after await() will be executed only when all the tasks are completed.

提交回复
热议问题