WaitHandle -- use Semaphore

Source: Internet
Author: User

WaitHandle -- use Semaphore

Semaphore also inherits from waithandle, which is used for the lock mechanism. Unlike Mutex, semaphore allows a specified number of threads to access resources at the same time. When the number of threads exceeds, semaphore waits in queue, until the previous thread exits.

Semaphore is suitable for high-concurrency scenarios such as web servers. It can limit the number of threads used to access resources.


Both Monitor and monitor have a lock holder, which is not required by semaphore. Therefore, sempahore is usually declared as static.

Let's look at the following example:

Namespace uses Semaphore {class Program {// The first parameter to specify the number of threads currently running to enter, and the second parameter to indicate the number of threads allowed to enter static Semaphore sem = new Semaphore (2, 2); static void Main (string [] args) {for (int I = 1; I <= 4; I ++) {new Thread (ThreadEntry ). start (I) ;}} static void ThreadEntry (object id) {Console. writeLine ("thread {0} wants to get in", id); sem. waitOne (); // when the waitone method is called, if there is a blank space, the placeholder is occupied; if there is no space, wait; Console. writeLine ("thread {0} gets in", id); Thread. sleep (1, 100); Console. writeLine ("thread {0} is leaving", id); sem. release (); // Release a blank space ;}}}

Call result:

Imagine that if there is a page for ticket snatching, all users can access the page concurrently at the same time, you can use this object to set the number of concurrent users and the number of concurrent users can enter the waiting state at the same time. Other people can route the queue to a queue. If the queue is too long, it will cause excessive server memory consumption. In this case, you can use a distributed method to allocate the queue to different hosts, reduces server pressure.

The above ideas have not been used in practice, but I have learned how to handle high concurrency issues for two days. This is also a reasonable idea.

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.