In Java thread, the Join () method is basically the code that calls the thread of the modified method to complete the contents of the Run method after the execution of the join () method. Example: Java code
- 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
- }
- }
If you remove T1.join () After this statement, the output of the book is chaotic, and after this statement, it will be output sequentially, in a sense to achieve synchronization.
Transferred from: http://auguslee.iteye.com/blog/1292203
Usage of Java Thread join ()