9

what's the best way to make main thread to wait until all threads are finished?

for(int i=0;i<n;i++){
   Thread t=new Thread();
   t.start();

}

//wait for all threads to finish

user121196
  • 30,032
  • 57
  • 148
  • 198

5 Answers5

21

Create a list and wait for the all.

List<Thread> threads = new ArrayList<Thread>();
for(int i=0;i<n;i++){
    Thread t=new Thread();
    t.start();
    threads.add(t);
}

for(Thread t: threads) t.join();

However using an ExecutorService can be a more elegant way to handle a pool of threads.

ExecutorService es = Executors.newCachedThreadPool();
for(int i=0;i<n;i++)
   es.submit(new Task(n));
es.shutdown();
es.awaitTermination(timeout, TimeUnit.SECONDS);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3
List<Thread> threads = new ArrayList<Thread>();

for(int i=0;i<n;i++){
   Thread t=new Thread();
   threads.add(t);
   t.start();

}


for(Thread t:threads){
t.join();
}
jmj
  • 237,923
  • 42
  • 401
  • 438
1

It can be done using thread.join( );.

It is also answered here already. Have a look.

Example :

class MyThread implements Runnable {
  String name; // name of thread

  Thread t;

  MyThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start();
  }

  public void run() {
    try {
      for (int i = 5; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
}

public class MainClass {
  public static void main(String args[]) {
    MyThread ob1 = new MyThread("One");
    MyThread ob2 = new MyThread("Two");
    MyThread ob3 = new MyThread("Three");

    System.out.println("Thread One is alive: " + ob1.t.isAlive());
    System.out.println("Thread Two is alive: " + ob2.t.isAlive());
    System.out.println("Thread Three is alive: " + ob3.t.isAlive());

    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
      ob3.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }

    System.out.println("Thread One is alive: " + ob1.t.isAlive());
    System.out.println("Thread Two is alive: " + ob2.t.isAlive());
    System.out.println("Thread Three is alive: " + ob3.t.isAlive());

    System.out.println("Main thread exiting.");
  }
}    

Although the question is tagged under "java", it can be also done in C# in following ways

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
0

You can use a CountDOwnLatch

CountDownLatch lat = new CountDownLatch(noOfTasks);

ExecutorService exec = Executors.newFixedThreadPool(4);

while(...) {

  exec.execute(new MyTask());

}


try {

  lat.await();

} 
catch (InterruptedException E) {

 //

}

and within your task (enclose in try / finally)

lat.countDown();
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
FatherMathew
  • 960
  • 12
  • 15
0

I think for your requirement the best solution is the CyclicBarrier look at http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html

or

at this example

Carmen
  • 6,177
  • 1
  • 35
  • 40