In many cases, the main process creates and starts a child thread, and if a child thread takes a lot of time-consuming operations, the main thread will tend to end before the child thread ends. At this point, if the main thread wants to wait for the child thread to finish before it finishes, such as a child thread processing a data, the main thread will have to get the value in this data, it will use the join () method. The function of the join () method is to wait for the thread object to be destroyed.
Package Org.github.lujiango;public class Test01 { static class MyThread extends Thread { @Override public void Run () { try { int sec = (int) (Math.random () * 10000); System.out.println (sec); Thread.Sleep (SEC); } catch (Exception e) { e.printstacktrace ();}} } public static void Main (string[] args) { try { MyThread t = new MyThread (); T.start (); T.join (); SYSTEM.OUT.PRINTLN ("I want to do when the T object is finished ..."); } catch (Exception e) { e.printstacktrace ();}} }
The function of a method join is to cause the owning thread object x to execute the task in the Run () method normally, and to cause the current thread z to block indefinitely, waiting for the thread X to destroy before continuing to execute the code after thread Z.
Method join has the effect of making threads queue up, and some are similar to the running effect of synchronization. The difference between join and synchronized is that join waits internally using the Wait () method, while the Sychronized keyword uses the object monitor principle as the synchronization.
signature of the Join method
The function of join is implemented using the wait (long) method, and all join methods have the feature of releasing the Lock:
Public final void Join () throws interruptedexception{ join (0); }
@param millis wait time, in milliseconds (if timed out, the blocked thread continues to execute because another thread joins)
Public final synchronized void join (long Millis) throws Interruptedexception {}
Join and exception
During the join process, if the current thread object is interrupted, an exception occurs for the current thread.
Package Org.github.lujiango;public class Test02 {static Class Threada extends Thread {@Override public void Run () {for (int i = 0; i < Integer.max_value; i++) {String newstring = new string (); Math.random (); }}}} Static class Threadb extends Thread {@Override public void run () {try { Threada a = new Threada (); A.start (); A.join (); System.out.println ("Thread B Run End ..."); } catch (Exception e) {System.out.println ("Thread B catch"); E.printstacktrace (); }}}} Static class Threadc extends Thread {private threadb threadb; Public THREADC (threadb threadb) {this.threadb = threadb; } @Override public void Run () {threadb.interrupt (); }} public static void Main (string[] args) { try {threadb b = new threadb (); B.start (); Thread.Sleep (500); THREADC C = new THREADC (b); C.start (); } catch (Exception e) {e.printstacktrace (); } }}
Join and interrupt methods if encountered, there will be an exception, but this time the button is still "red", because thread A is still running, a is an exception, is the state of normal execution.
Multithreading-join () method