The Java thread class has a join () method, which has not been known for a long time until you see this article. http://auguslee.iteye.com/blog/1292203
In Java thread, the Join () method is primarily to let the thread that invokes the method complete the contents of the Run method and then execute the code after the join () method. Example:
Threadtesterb in the Threadtestera.join () is blocked after it is called and continues until Threadtestera execution is complete.
[Java]View Plaincopy
- Class Threadtestera implements Runnable {
- private int counter;
- @Override
- public Void Run () {
- While (counter <= ) {
- System.out.print ("Counter =" + Counter + ");
- counter++;
- }
- System.out.println ();
- }
- }
- Class Threadtesterb implements Runnable {
- private int i;
- @Override
- public Void Run () {
- While (i <= ) {
- System.out.print ("i =" + i + "");
- i++;
- }
- System.out.println ();
- }
- }
- Public class Threadtester {
- public static void Main (string[] args) throws interruptedexception {
- Thread T1 = new Thread (new Threadtestera ());
- Thread t2 = new Thread (new Threadtesterb ());
- T1.start ();
- T1.join (); //wait T1 to be finished
- T2.start ();
- T2.join (); //In the This program, this is May removed
- }
- }
After the T1 is started, the join () method is called until the T1 count task ends and the T2 starts, and then T2 also starts counting the tasks. As you can see, in the instance, two threads are executed in strict order.
If the execution of T2 depends on the complete data in T1, this method can ensure the synchronization of the two threads well.
Usage of Thread Join ()