Multithreading, threading control concurrency

Source: Internet
Author: User
Tags throw exception

 private   AutoResetEvent exitevent;  private  void   Processa () { while  (true  ) {Console.WriteLine ( " 
    do some thing   "        if   (Exitevent.waitone (waitTime)) {break  ; }} Console.WriteLine (  " exit   " );} Exitevent.set ();  
Private void Processb () {   while (true)   {       Console.WriteLine ('do  Some thing");      Thread.Sleep (WaitTime)   }}

About the threading small model, there are two processing functions, one is a simple sleep wait, one is the semaphore, but there is a flaw, that is, whether it is executed at the time (do some thing) cannot exit. The signal volume is better, can control the exit when waiting, sleep words can only throw exception to exit (or set flag in while, but same).

 Public New Semaphoreslim (ten); // concurrency limit is ten

In a multithreaded model, you can control the number of threads to control concurrency.

= =   {Console.WriteLine ("dosome thing"); Semaphoreslim.release ();});

Suitable for a large number of threads to do the same thing to control concurrency, pretty useful

Multithreading, threading control concurrency

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.