Thread. Join joins the specified thread into the current thread, and you can combine two alternating threads into sequential execution threads. For example, the join () method of thread A is invoked in line B until thread a completes and execution of thread B continues.
T.join (); Causes the calling thread T to finish before this.
T.join (1000); Wait for t thread, wait time is 1000 milliseconds
First in the JDK code: Java code /** * waits at most <code>millis</code> milliseconds for this thread to * die. A timeout of <code>0</code> means to wait forever. */ //here a timeout of 0 means to wait forever The literal meaning is to wait forever, actually wait until T is over. public final synchronized void join (Long millis) throws InterruptedException { long base = system.currenttimemillis (); long now = 0; if (millis < 0) { throw new illegalargumentexception ("Timeout value is negative "); } if (millis == 0) { while (isAlive ()) { wait (0); } } else { while (IsAlive ()) { long delay = millis - now; if (delay <= 0) { break; } wait (delay); now = system.currenttimemillis () - base; } } } &nbsP
From the Code view, if the thread is generated, but has not been started, calling its join () method is not useful, and will continue directly down execution
The Join method implementation is achieved by a wait (small hint: The method provided by Object). When the main thread calls T.join, the main thread obtains a lock on the thread object T (wait means to get the lock on the object), calls the object's waiting time, until the object wakes up the main thread, such as after exiting. This means that the main thread must be able to get a lock on the thread T object when calling T.join
Example1: Java Code public class jointest implements runnable{ public static int a = 0; public void run () { for (int k = 0; k < 5; k++) { a = a + 1; } } public static void main (String[] args) throws Exception { Runnable r = New jointest (); thread t = new threaD (r); t.start (); system.out.println (a); } }
Does the output of the program result in 5? The answer is: it's possible. In fact, it is difficult for you to encounter output 5, usually not 5. Of course, it has a serious relationship with the machine. Why, then? My explanation is that when the main thread Main method executes System.out.println (a), the thread has not really started running, perhaps it is allocating resources for it to run. Because assigning a resource to a thread takes time, the main method continues to execute SYSTEM.OUT.PRINTLN (a) after executing the T.start () method, and this time the result is a value 0 which has not been changed. How to make the output 5. In fact, the join () method provides this functionality. The join () method, which enables the thread that invoked the method to execute before it completes. Java Code public static void main (String[] args) throws exception { Runnable r = new Jointest (); Thread t = new Thread (R); t.start (); t.join () //join join () system.out.println (a); }
This time, the program input result is always 5.
To prove that if the T.join () method is not used, the SYSTEM.OUT.PRINTLN (a) of the main thread main method is executed first, and we can add a loop to the main method that extends the time of the main method execution. The number of cycles will depend heavily on machine performance. We can also see that the output of a is 5 if the number of loops is proper. Java Code public static void main (String[] args) throws exception { Runnable r = new Jointest (); Thread t = new Thread (R); t.start (); //t.join () //join join () /* Note that the loop body must have actual execution statements, otherwise the compiler or JVM might optimize your code, depending on the paragraph code is invalid. */ for (int i=0; i<300; i++) { system.out.print (i); } System.out.println (); system.out.println (a); }
Tested by yourself, the last a was always 5.
This example refers to: http://agio.iteye.com/blog/210600
example2:join (n) Java Code class runnableimpl implements runnable { public void run () { try { System.out.println ("Begin sleep"); thread.sleep (1000); system.out.println ("End sleep"); } catch (interruptedexception e) { e.printstacktrace (); } } } Java code Public class jointest{ public static void main (String[] args) { thread t = new thread (New runnableimpl ()); t.start (); try { t.join (1000); system.out.println ("Joinfinish"); } catch (interruptedexception e) { e.printstacktrace (); } } }&Nbsp;
The
result is:
Begin sleep
End sleep
Joinfinish
See, when the main thread calls T.join, the main thread waits for the T thread, the wait time is 1000, if the T thread is sleep 2000 Java code Class runnableimpl implements Runnable { public void run () { try { system.out.println ("Begin sleep"); thread.sleep ( //); originally 1000 system.out.println ("End Sleep "); } catch (interruptedexception e) { E.printstacktrace (); } }   }
The result:
Begin Sleep
Joinfinish
End Sleep
This means that the main thread waits only 1000 milliseconds, regardless of when t ends.
Reference: Http://blog.csdn.net/FG2006/archive/2011/05/04/6393768.aspx
Example3: Java Code class customthread1 extends thread { public void run () { string threadname = thread.currentthread ( ). GetName (); system.out.println ( threadname + " start"); try { for (int i = 0; i < 5; i++) { system.out.println (threadname + " loop at " + i); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp; thread.sleep (1000); } system.out.println (threadname + " end."); } catch (exception e) { System.out.println ("exception from " + threadName + ". Run"); } } } class customthread extends thread { CustomThread1 t1; public  Customthread (CUSTOMTHREAD1&NBSP;T1) { this.t1 = t1; } public void run () { String threadName = Thread.CurrentThread () getName (); System.out.println (threadname + " start."); try { t1.join (); system.out.println (threadName + " end."); } catch (exception e) { system.out.println ("exception from " + threadname + ". Run"); } } } Public class JoinTestDemo { public Static void main (String[] args) { string threadname = thread.currentthread (). GetName (); system.out.println (threadname + " start."); customthread1 t1 = new customthread1(); customthread t = new customthread (t1); try { t1.start (); thread.sleep ( ; t.start ( ); t.join (); //in code 2, release this note } catch (exception e) { system.out.println ("Exception from main"); } system.out.println ( threadname + " end!"); } }
Results:
The
Main start. //main the thread on which the method is started, but does not end immediately because the call to T.join (), so wait until T is over before this thread can execute down. &NBSP
[CustomThread1] thread start. //threads CustomThread1 start
[CustomThread1] Thread loop at 0 /threads CustomThread1 execution
[CustomThread1] thread loop at 1 //thread CustomThread1 execution
[Customthread] thread start. //threads Customthread started, but did not end immediately because the call to T1.join (); So until the T1 is finished, this thread can be executed down. &NBSP
[CustomThread1] thread loop at 2 //thread CustomThread1 continue to execute
[CustomThread1] thread loop at 3 / /thread CustomThread1 continue execution of
[CustomThread1] thread loop at 4 //threads CustomThread1 continue to execute
[CustomThread1] Thread end. //Threads CustomThread1 ended
[customthread] Thread end. //thread customthread The result the T1.join (), the start of the block, and the continuation of the execution down;
Main end! //Thread customthread end, this thread at T.join (), blocking the start, down to continue execution of the results.
Comment out the join in the example above: Java code