Wait until child threads completed : Java

前端 未结 6 749
被撕碎了的回忆
被撕碎了的回忆 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:45

    In Java 8 a far better approach is to use parallelStream()

    Note: it is far easier to see exactly what these background tasks are doing.

    public static void main(String[] args) {
        Stream.of(
             () -> mytest.result.setIntValue(346635),
             () -> mytest.result.setStringValue("Hello hi"),
             () -> mytest.result.setBoolValue(true) )
             .parallel()
             .forEach(Runnable::run);
    
        System.out.println("main finished");
        System.out.println("Result is : " + mytest.result.toString());
    }
    

    I took out the debug information and the sleep as these don't alter the outcome.

提交回复
热议问题