Join method for multithreading thread in C #

Source: Internet
Author: User

When multi-threading is used in our programs, the operation of the resource requires special attention. As in the following example: There are two string objects in the program (the initial value is empty), the main function opens two threads respectively to set the values of the two string objects, and then prints the values of the two strings in the main function. The code is as follows:

        Static stringSTR1 =string.        Empty; Static stringSTR2 =string.        Empty; Static voidMain (string[] args) {Thread Worker1=NewThread (SetResource1); Thread Worker2=NewThread (SETRESOURCE2); Worker1.            Start (); Worker2.            Start (); Console.WriteLine ("Result:"+ str1 +" "+str2); }        Static voidSetResource1 () {Thread.Sleep (Timespan.fromseconds (2)); STR1="Hello"; }        Static voidSetResource2 () {Thread.Sleep (Timespan.fromseconds (3)); STR2=" World"; }

Program Run Result:

Continue ...

You can see that the main function does not print out the values of str1 and str2. Because the threads Worker1 and Worker2 took 2 seconds and 3 seconds to set the values for STR1 and str2 respectively. When the main function prints the values of str1 and STR2, the two worker threads have not set the values well, so the values for str1 and STR2 cannot be printed in main.

To solve this problem, we need to wait for both Worker1 and worker2 in the main function to finish, then print the value of two strings, at which point we can use the Join function in the thread class. Join means to wait for the thread to finish executing, or to block the main thread (the one on which the main function is located).

        Static stringSTR1 =string.        Empty; Static stringSTR2 =string.        Empty; Static voidMain (string[] args) {Thread Worker1=NewThread (SetResource1); Thread Worker2=NewThread (SETRESOURCE2); Worker1.            Start (); Worker2.            Start (); Worker1.            Join (); Console.WriteLine ("the value of STR1 has been set!"); Worker2.            Join (); Console.WriteLine ("the value of STR2 has been set!"); Console.WriteLine ("Result:"+ str1 +" "+str2); }        Static voidSetResource1 () {Thread.Sleep (Timespan.fromseconds (2)); STR1="Hello"; }        Static voidSetResource2 () {Thread.Sleep (Timespan.fromseconds (3)); STR2=" World"; }

The results of the operation are as follows:

The value of STR1 has been set! the value of str2 has been set !  Continue ...

As you can see from the code, we call the join method of Worker1 and Worker2 separately in the main function, and the result is that the main function will continue to execute after the Woker1 and Worker2 are executed, otherwise the main function is in a blocking (waiting) state. Join also has an additional overloaded method that specifies the maximum time to wait (for example, 3 seconds), and the main function continues to execute if the maximum wait time is exceeded.

The author encountered a scene in the work: the code of the site needs to call through the background of multiple stored procedures, and each stored procedure will be time-consuming, so we use multithreading technology to call the stored procedure separately. The Join method is then called on each thread in the code, waiting for each stored procedure result to be returned, and then encapsulating the data to the foreground UI (User Interface). This improves the operation efficiency of the program and ensures the integrity of the data.

Join method for multithreading thread in C #

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.