Meaning of the Join method in multi-threading

Source: Internet
Author: User

1, Function: Call this method, the main process will stop here, waiting for the thread to finish before continuing to execute.

For example: Do not use join:

<?phpclass Join extends thread{public    function run ()    {        sleep (3);        Echo __function__. Php_eol;}    } $join = new Join (), $join->start (); Echo ' Just a test '. Php_eol;

Output:

Just a TestRun

Using join:

<?phpclass Join extends thread{public    function run ()    {        sleep (3);        Echo __function__. Php_eol;}    } $join = new Join (), $join->start (); $join->join (); Echo ' Just a test '. Php_eol;

Output:

Runjust a test

We can see that when using join, the last Echo statement needs to wait for the thread to finish running.

2. Why Join?

Think of a situation in which thread execution takes too long and the result is to be used in subsequent code to run the thread.

This allows us to use join to prevent the end of the mainline Cheng line enters upgradeable, or the subsequent code to run before the thread ends to get the results of the threading process.

3. Using join causes the process to block, which is not an end, but a performance. Join is not used to block threads.

4, Fork/join

Meaning of the Join method in multi-threading

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.