The Java main thread waits for all child threads to execute, in fact, in our work often used, such as the main thread to return a response to the user's value, but this is worth the assignment process is done by the child thread (simulation of a real development scenario), so the main thread must wait for the child thread to complete, Respond to the user again, otherwise, the response user is a meaningless value.
So how do you make sure that all the child threads are finished. In general, there are the following methods:
1 Let the main thread wait, or sleep for a few minutes. With Thread.Sleep () or TimeUnit.SECONDS.sleep (5);
As follows:
Package Andy.thread.traditional.test;import java.util.concurrent.timeunit;/** * @author zhang,tianyou * @version November 21, 2014 PM 11:15:27 */public class ThreadSubMain1 {public static void main (string[] args) {//TODO auto-generated Metho d stubfor (int i = 0; i <; i++) {New Thread (new Runnable () {public void run () {try {thread.sleep (1000);//Impersonation Sub-thread task} C Atch (interruptedexception e) {}system.out.println ("sub-thread" + thread.currentthread () + "executed")}). Start ();} try {//wait for all child threads to complete TimeUnit.SECONDS.sleep (5);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Mainline execution. ");}}
The effect is as follows:
Child thread Thread[thread-1,5,main] executes the child thread Thread[thread-3,5,main] completes the child thread Thread[thread-5,5,main] executes the child thread thread[thread-7,5 , main] completes the child thread Thread[thread-9,5,main] executes the child thread Thread[thread-0,5,main] executes the child thread Thread[thread-2,5,main] completes the child thread thread[ Thread-4,5,main] Executes the child thread Thread[thread-6,5,main] executes the child thread Thread[thread-8,5,main] execution completes the main line execution.
The main thread of this party is only sleeping for 5 seconds, but there is no guarantee that all the sub-threads will complete, so 5 seconds here is just a valuation.
2 join with Thread () waits for all child threads to complete, the main thread executes
The implementation is as follows:
Package Andy.thread.traditional.test;import java.util.vector;/** * @author zhang,tianyou * @version November 21, 2014 PM 11 : 15:27 */public class ThreadSubMain2 {public static void main (string[] args) {//Use thread-safe Vector vector<thread> Thread s = new vector<thread> (); for (int i = 0; i <, i++) {thread ithread = new Thread (new Runnable () {public void Ru N () {try {thread.sleep (1000);//simulate Sub-thread task} catch (Interruptedexception e) {}system.out.println ("Child thread" + Thread.CurrentThread () + "execution complete");}); Threads.add (Ithread); Ithread.start ();} for (Thread ithread:threads) {try {///wait for all threads to finish executing ithread.join ();} catch (Interruptedexception e) {e.printstacktrace ()}} SYSTEM.OUT.PRINTLN ("Mainline execution. ");}}
The results of the implementation are also as follows:
Child thread Thread[thread-1,5,main] executes the child thread Thread[thread-2,5,main] completes the child thread Thread[thread-0,5,main] executes the child thread thread[thread-3,5 , main] completes the child thread Thread[thread-4,5,main] executes the child thread Thread[thread-9,5,main] executes the child thread Thread[thread-7,5,main] completes the child thread thread[ Thread-5,5,main] Executes the child thread Thread[thread-8,5,main] executes the child thread Thread[thread-6,5,main] execution completes the main line execution.
This approach meets the requirements, and it can wait until all the child threads are executed before the main thread executes.
3 Use the Executorservice thread pool to wait for all tasks to complete before executing the main thread,awaittermination.
awaitTermination(long timeout,TimeUnit unit)
A request is closed, a timeout occurs, or when the front thread is interrupted, whichever occurs first, it will cause blocking until all tasks have completed execution.
Package Andy.thread.traditional.test;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.timeunit;/** * @author zhang,tianyou * @version November 21, 2014 PM 11:15:27 */public class ThreadSubMain3 {public static void main (string[] args) {//define a buffered thread value thread pool size according to task change ex Ecutorservice ThreadPool = Executors.newcachedthreadpool (); for (int i = 0; i <; i++) {Threadpool.execute (new runnabl E () {public void run () {try {thread.sleep (1000);//Impersonation Sub-thread task} catch (Interruptedexception e) {}system.out.println ("child thread" + Th Read.currentthread () + "execution complete");}); Starts a sequential shutdown, performs a previously submitted task, but does not accept new tasks. Threadpool.shutdown (); try {//Request Close, time out, or when the front thread is interrupted, whichever occurs first, will cause blocking until all tasks complete execution// Set maximum wait 10 seconds threadpool.awaittermination (ten, timeunit.seconds);} catch (Interruptedexception e) {//e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Mainline execution. ");}}
The results of the implementation are as follows:
Child thread Thread[pool-1-thread-4,5,main] execution complete child thread Thread[pool-1-thread-1,5,main] execution complete child thread Thread[pool-1-thread-7,5,main] Execution complete child thread Thread[pool-1-thread-6,5,main] Complete child thread Thread[pool-1-thread-5,5,main] Execute child thread Thread[pool-1-thread-2,5,main ] Executes the child thread Thread[pool-1-thread-3,5,main] completes the child thread Thread[pool-1-thread-8,5,main] executes the child thread thread[pool-1-thread-10,5, Main] Executes the thread execution complete Thread[pool-1-thread-9,5,main] execution completes.
This method, like Method 2, waits until all the child threads have finished executing the main thread.
Java main thread waits for all child threads to complete before executing the workaround set