Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Main thread wait for other threads

After seaching a lot and not finding a concrete answer

If I have two threads started:

Thread t1 = new Thread();
Thread t2 = new Thread();

t1.start();
t2.start();

After starting the threads I need the main thread to wait for these two thread to finish before printing the final result

How can I make the main thread wait for both t1 and t2?

A plain wait() would be enough?

like image 512
SaintLike Avatar asked Jun 23 '26 09:06

SaintLike


1 Answers

Add

t1.join();
t2.join();

in your thread which should wait till t1 and t2 will finish their tasks (in your case call it from your main thread).

like image 162
Pshemo Avatar answered Jun 26 '26 22:06

Pshemo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!