The Java main thread waits for all child threads to finish executing. In fact, in our work is often used, such as the main thread to return a response to the user's value, but the value of the assignment process is done by the child thread (simulation of an actual development of the situation), so the main thread must wait for the child to complete the execution, In response to the user, otherwise, the response to the user is a meaningless value.
So how do you make sure all of the child threads are finished. Generally, 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 a stub for
(int i = 0; i < i++) {
New Thread (New Runnable () {public
void run () {
try {
thread.sleep (1000);
Simulate child thread task
} catch (Interruptedexception e) {
}
System.out.println ("Child" + thread.currentthread () + " Execution completed ");
Start ();
}
try {/
/wait for all child threads to finish
TimeUnit.SECONDS.sleep (5);
\ catch (interruptedexception e) {
//TODO Auto-generated Catch block
e.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Mainline execution.") ");
}
}
The effect is as follows:
Child thread Thread[thread-1,5,main] execution complete child thread
Thread[thread-3,5,main] Execute
child thread Thread[thread-5,5,main] execution completed
Child thread Thread[thread-7,5,main] execution complete child thread
Thread[thread-9,5,main] Execute
child thread Thread[thread-0,5,main] execution completed
Child thread Thread[thread-2,5,main] execution complete child thread
Thread[thread-4,5,main] Execute
child thread Thread[thread-6,5,main] execution completed
Child thread Thread[thread-8,5,main] execution complete
mainline execution.
This main thread only sleeps for 5 seconds, but it does not guarantee that all of the child threads will perform, so 5 seconds here is just a valuation.
2 using thread join () waits for all child threads to complete, the main thread is executing
Implemented 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> threads = new Vector<thread > ();
for (int i = 0; i < i++) {
thread ithread = new Thread (new Runnable () {public
void run () {
try {
thread.sleep (1000);
Simulate child thread task
} catch (Interruptedexception e) {
}
System.out.println ("Child" + thread.currentthread () + " Execution completed ");
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 as follows:
Child thread Thread[thread-1,5,main] execution complete child thread
Thread[thread-2,5,main] Execute
child thread Thread[thread-0,5,main] execution completed
Child thread Thread[thread-3,5,main] execution complete child thread
Thread[thread-4,5,main] Execute
child thread Thread[thread-9,5,main] execution completed
Child thread Thread[thread-7,5,main] execution complete child thread
Thread[thread-5,5,main] Execute
child thread Thread[thread-8,5,main] execution completed
Child thread Thread[thread-6,5,main] execution complete
mainline execution.
This is in line with the requirement that it can wait for all the child threads to finish and the main thread will not execute.
3 Use the Executorservice thread pool, wait for all tasks to complete and then execute the main thread,awaittermination.
awaittermination (Long timeout,timeunit Unit)
Request shutdown, timeout, or when the front end is interrupted, will cause blocking until all tasks are completed.
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 Ma
In (string[] args) {//define the size of a buffered thread pool based on task changes executorservice ThreadPool = Executors.newcachedthreadpool (); for (int i = 0; i < i++) {Threadpool.execute (New Runnable ()} {public void run () {try {
. Sleep (1000); Simulate child thread task} catch (Interruptedexception e) {} System.out.println ("sub-thread" + thread.currentthread () + "execute complete")
;
}
});
//Start a sequential shutdown, perform previously committed tasks, but do not accept new tasks.
Threadpool.shutdown (); try {//request shutdown, timeout, or when the line is interrupted, whichever occurs first, will cause blocking until all tasks are completed/set to wait 10 seconds threadpool.awaittermination (timeunit.se
Conds);
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 completed
child thread Thread[pool-1-thread-1,5,main] execution
child thread thread[pool-1-thread-7, 5,main] Finished child thread
thread[pool-1-thread-6,5,main] Execution child thread
Thread[pool-1-thread-5,5,main] execution
child thread thread[ Pool-1-thread-2,5,main] finished
child thread Thread[pool-1-thread-3,5,main] execution
child thread Thread[pool-1-thread-8,5,main] Completion of the
child thread Thread[pool-1-thread-10,5,main] execution of the
child thread Thread[pool-1-thread-9,5,main] execution completed
mainline execution.
This method, like Method 2, waits until all child threads have finished executing before executing the main thread.