thread provides a way to--join a thread to wait for another thread to complete. When a join method of another thread is invoked in a program execution stream, the calling thread is blocked, knowing that the join thread joined by the Join method is finished executing.
Join methods are usually called by programs that use threads to divide large problems into small problems, each of which assigns a thread. When all the minor problems are processed, the main thread is invoked to further operate.
Package com.hehe.thread;
public class Join extends thread{public
join (String name) {
super (name);
}
Override the Run method to define the thread executor public
void run () {for
(int i = 0; i < i++) {
System.out.println (getName () + "" + i) ;
}
}
public static void Main (string[] args) throws Exception {
//boot child thread new
Join ("New Thread"). Start ();
for (int i = 0; i < i++) {
if (i==5) {
join jointhread=new join ("thread being join");
Jointhread.start ();
The main thread invokes the Join method on the Jointhread field, and the main thread must wait until Jointhread execution finishes before executing the
jointhread.join ();
System.out.println (Thread.CurrentThread (). GetName () + "" + i);}}}
There are 3 threads in the above program, and the main method starts with a child thread called "new thread" that will execute concurrently with the Mian thread. When the main thread's loop variable I equals 5 o'clock, the thread named "Join Thread" is started, the threads are not executed concurrently with the main threads, and the main thread must wait for the thread to finish execution before it can be executed down. When a thread named "Threaded by join" executes, only 2 child threads are actually executing concurrently, while the main thread is in a waiting state.
Three overloaded forms of the Join method:
1.join (): Waiting for a join thread to execute.
2.join (Long Millis): The maximum time for a thread waiting to be join is millis milliseconds. If the thread that was join in Millis milliseconds has not finished executing, it is no longer waiting.
3.join (Long Millis,int Nanos): The maximum time for a thread waiting to be join is Millis millisecond plus Nanos nanosecond.