Wait until child threads completed : Java

前端 未结 6 746
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 04:08

Problem description : -

Step 1: Take input FILE_NAME from user at main thread.

Step 2: Perform 10 operations on

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 04:56

    I would recommend looking at the Executors framework first, and then look into the CompletionService.

    Then you can write something like this:

    ExecutorService executor = Executors.newFixedThreadPool(maxThreadsToUse);
    CompletionService completion = new ExecutorCompletionService(executor);
    for (each sub task) {
        completion.submit(new SomeTaskYouCreate())
    }
    // wait for all tasks to complete.
    for (int i = 0; i < numberOfSubTasks; ++i) {
         completion.take(); // will block until the next sub task has completed.
    }
    executor.shutdown();
    

提交回复
热议问题