C # synchronous function running using multiple threads

Source: Internet
Author: User

We suppose there are methods run1 () and run2 (), which consume a lot of time. Implementing their synchronous operation will greatly improve the program efficiency. Here we will consider using multi-threaded methods. First, add a reference to define bool type I. The value j is false. [Csharp] using System. Threading; In the function entry, for example, start. [Csharp] void start () {// instantiate the new class to be enabled, Thread thread1 execution method run1 Thread thread1 = new Thread (run1); // instantiate the new class to be enabled, thread thread2 execution method run2 Thread thread2 = new Thread (run2); while (true) // determine whether run1 and run2 are completed {if (I = true & j = true) {break;} else {Thread. sleep (10); // if the method execution is slow, you can add }}to define the methods of run1 and run2 outside [csharp] public void run1 () {// do someting I = true;} public void run2 () {// do someting j = true ;}

Related Article

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.