Wait until child threads completed : Java

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

    You can do:

    Thread t = new Thread() {
        public void run() {
            System.out.println("text");
            // other complex code
        }
     };
     t.start();
     t.join();
    

    This way you will wait until the thread finishes and just then continue. You can join multiple threads:

    for (Thread thread : threads) {
      thread.join();
    }
    

提交回复
热议问题