Use of the Java thread join () method

Source: Internet
Author: User
Run () and start () are two familiar methods. Put all the code to be processed in parallel in run (); Stat () is used to automatically call run (), which is defined by the internal mechanism of Java. And the access control Letter of run () must be public, and the return value must be void (this statement is not accurate, run () does not return values). Run () does not contain parameters.

Let's talk less about join (). Let's first look at an example:

Code:


/**
* @author QingHe
* Creation on 2005-12-19
*/

public class ThreadTest 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 ThreadTest();
Thread t = new Thread(r);
t.start();
System.out.println(a);
}
}

Is the output result of the program 5? The answer is: possible. In fact, it is difficult for you to output 5, which is usually not 5. Of course, this also has a serious relationship with machines. Why? My explanation is that when the main thread main method executes the system. Out. println (a); Statement, the thread has not actually started running. Maybe it is allocating resources for it to prepare for running. It takes time to allocate resources to the thread, and the main method is executed completely. start () method and continue to execute system. out. println (a); at this time, the result is that a has not been changed to 0. How can I set the output result to 5! In fact, the join () method provides this function. Join ()
Method, which enables the thread that calls this method to complete execution before this.

Code:


/**
* @author QingHe
* Creation on 2005-12-19
*/

public class ThreadTest 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 ThreadTest();
Thread t = new Thread(r);
t.start();
t.join();
System.out.println(a);
}
}

At this time, the input result of the program is always 5.

To prove that T. join () method, the main thread main method system. out. println (a); the statement will be executed first. We can add a loop to the main method, which is used to prolong the time for executing the main method. The number of cycles will depend heavily on the machine performance. If the number of loops is correct, we can also see that the output result of A is 5.

Code:

/**
* @ Author Qinghe
* Creation on 2005-12-19
*/

Public class threadtest 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 threadtest ();
Thread t = new thread (R );
T. Start ();
For (INT I = 0; I <300; I ++ ){
/*
Note that the loop body must have actual execution statements. Otherwise, the compiler or JVM may optimize your code.
The code is invalid.
*/
System. Out. Print (I );
}
System. Out. println ();
System. Out. println ();
}
}

At this time, on my machine, the output value of A is always 5. Run () and start () are two familiar methods. Put all the code to be processed in parallel in run (); Stat () is used to automatically call run (), which is defined by the internal mechanism of Java. And the access control Letter of run () must be public, and the return value must be void (this statement is not accurate, run () does not return values). Run () does not contain parameters.

Let's talk less about join (). Let's first look at an example:

Code:


/**
* @author QingHe
* Creation on 2005-12-19
*/

public class ThreadTest 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 ThreadTest();
Thread t = new Thread(r);
t.start();
System.out.println(a);
}
}

Is the output result of the program 5? The answer is: possible. In fact, it is difficult for you to output 5, which is usually not 5. Of course, this also has a serious relationship with machines. Why? My explanation is that when the main thread main method executes the system. Out. println (a); Statement, the thread has not actually started running. Maybe it is allocating resources for it to prepare for running. It takes time to allocate resources to the thread, and the main method is executed completely. start () method and continue to execute system. out. println (a); at this time, the result is that a has not been changed to 0. How can I set the output result to 5! In fact, the join () method provides this function. Join ()
Method, which enables the thread that calls this method to complete execution before this.

Code:


/**
* @author QingHe
* Creation on 2005-12-19
*/

public class ThreadTest 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 ThreadTest();
Thread t = new Thread(r);
t.start();
t.join();
System.out.println(a);
}
}

At this time, the input result of the program is always 5.

To prove that T. join () method, the main thread main method system. out. println (a); the statement will be executed first. We can add a loop to the main method, which is used to prolong the time for executing the main method. The number of cycles will depend heavily on the machine performance. If the number of loops is correct, we can also see that the output result of A is 5.

Code:

/**
* @ Author Qinghe
* Creation on 2005-12-19
*/

Public class threadtest 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 threadtest ();
Thread t = new thread (R );
T. Start ();
For (INT I = 0; I <300; I ++ ){
/*
Note that the loop body must have actual execution statements. Otherwise, the compiler or JVM may optimize your code.
The code is invalid.
*/
System. Out. Print (I );
}
System. Out. println ();
System. Out. println ();
}
}

At this time, on my machine, the output value of A is always 5.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.