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
Thread. sleep() method can be used to pause the execution of current thread for specified time in milliseconds.
The statement “Thread. currentThread(). join()”, will tell Main thread to wait for this thread(i.e. wait for itself) to die.
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.
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.
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!");
You could share a CyclicBarrier
object among your RaceCar
s 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 RaceCar
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With