当前位置:网站首页>How to make the main thread wait for the sub thread to execute before executing

How to make the main thread wait for the sub thread to execute before executing

2022-06-26 06:24:00 It's very quiet

Summarize the three methods :

(1)join Method , Direct each thread to join Just go in .

(2)countdownlatch, Internally, a counting operation is maintained , adopt cas The algorithm guarantees the atomicity of the operation , When the thread has finished executing , call countDown() Method , Subtract one from the count , Main thread call countdownlatch Object's await() Method , Suspends the current thread , until countdownlatch The count of objects is 0, Main thread execution .

(3) Something like a barrier , When the sub thread is finished , call CyclicBarrier Object's await() Method , Until a specified number of threads reach the barrier , Will continue to execute the main thread . Compare each child thread to && A condition of a symbol , Then only when all the conditions are met , To execute the main thread .

原网站

版权声明
本文为[It's very quiet]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260605279035.html