This is a newbie question. I start 10 threads from my main thread. How do I stop the main thread from continuing until all the other threads finish?
Waiting for existing threads to complete their execution can be achieved by using the awaitTermination() method.
So, if we want that the main thread should wait until all the other threads are finished then there is a function pthread_join(). #include <pthread. h> int pthread_join(pthread_t thread, void **rval_ptr); The function above makes sure that its parent thread does not terminate until it is done.
Simply put, calling wait() forces the current thread to wait until some other thread invokes notify() or notifyAll() on the same object. For this, the current thread must own the object's monitor.
join() is a method in Java that causes the current thread to pause execution until the specified thread terminates. This is useful when we want to wait for a specific thread to complete before continuing the execution of the current thread.
Join all threads:
for t in threads:
t.join()
Here threads
is the list of your threads.
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