Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pause main() until all other threads have died?

Tags:

In my program, I am creating several threads in the main() method. The last line in the main method is a call to System.out.println(), which I don't want to call until all the threads have died. I have tried calling Thread.join() on each thread however that blocks each thread so that they execute sequentially instead of in parallel.

Is there a way to block the main() thread until all other threads have finished executing? Here is the relevant part of my code:

public static void main(String[] args) {  //some other initialization code  //Make array of Thread objects Thread[] racecars = new Thread[numberOfRaceCars];  //Fill array with RaceCar objects for(int i=0; i<numberOfRaceCars; i++) {     racecars[i] = new RaceCar(laps, args[i]); }  //Call start() on each Thread for(int i=0; i<numberOfRaceCars; i++) {     racecars[i].start();     try {         racecars[i].join(); //This is where I tried to using join()                             //It just blocks all other threads until the current                                                         //thread finishes.     } catch(InterruptedException e) {         e.printStackTrace();     } }  //This is the line I want to execute after all other Threads have finished System.out.println("It's Over!");  } 

Thanks for the help guys!

Eric

like image 867
ericso Avatar asked Mar 12 '10 21:03

ericso


People also ask

How do you pause the main thread?

Thread. sleep() method can be used to pause the execution of current thread for specified time in milliseconds.

How do I make main thread wait for other threads?

The statement “Thread. currentThread(). join()”, will tell Main thread to wait for this thread(i.e. wait for itself) to die.

How Stop main thread while another thread is running?

You can not stop the main thread while any other thread are running. (All the child threads born out of main thread.) You can use function Thread. join() to keep the main thread waiting while other thread(s) execute.

How do I make main thread wait for other threads in python?

Use the Python threading module to create a multi-threaded application. Use the Thread(function, args) to create a new thread. Call the start() method of the Thread class to start the thread. Call the join() method of the Thread class to wait for the thread to complete in the main thread.


2 Answers

You start your threads and immediately wait for them to be finished (using join()). Instead, you should do the join() outside of the for-loop in another for-loop, e.g.:

// start all threads for(int i=0; i<numberOfRaceCars; i++) {     racecars[i].start(); } // threads run... we could yield explicity to allow the other threads to execute // before we move on, all threads have to finish for(int i=0; i<numberOfRaceCars; i++) {     racecars[i].join(); // TODO Exception handling } // now we can print System.out.println("It's over!"); 
like image 52
middus Avatar answered Oct 13 '22 19:10

middus


You could share a CyclicBarrier object among your RaceCars and your main thread, and have the RaceCar threads invoke await() as soon as they are done with their task. Construct the barrier with the number of RaceCar threads plus one (for the main thread). The main thread will proceed when all RaceCars have finished. See http://java.sun.com/javase/6/docs/api/java/util/concurrent/CyclicBarrier.html

In detail, construct a CyclicBarrier in the main thread, and add a barrier.await() call in your RaceCar class just before the run() method exits, also add a barrier.await() call before the System.out.println() call in your main thread.

like image 39
the-banana-king Avatar answered Oct 13 '22 18:10

the-banana-king