Thread token, multi-thread token bucket

Source: Internet
Author: User

Thread token, multi-thread token bucket

Recently, I encountered a headache in the project. I wrote a broadcast thread in the frontend connection event. The broadcast thread is written in the while LOOP, but there are many clients in the foreground, when a connection is not established, a broadcast thread is enabled, which consumes resources. At the beginning, the method for solving this problem is to check the current number of connections each time a connection event is triggered. If the connection is count_client <= 1, open the thread. Otherwise, the broadcast is skipped. However, this method is only applicable to Connection events. If the broadcast thread is enabled Based on the received event, therefore, the number of connections cannot be used to determine whether to enable broadcast. I have been thinking for a long time and think of the concept of token in the algorithm, which can be used here.

Set a global thread variable tokenThread in the program, which is equivalent to a token. The thread with the token has the permission to enable the thread. When the thread with the receipt token is being executed, if other threads want to enable the feature, the feature will be rejected. Only other threads can enable the feature when the current execution thread ends and the token is released.

I wrote a demo simply because it was my own method. If you find any deficiencies in the demo, please point it out. Thank you.

The demo is as follows:

1 static Thread tokenThread = null; // Thread token 2 static int I = 0; // control broadcast end 3 public static void Greating () 4 {5 6 Thread th = new Thread () => 7 {8 I = 0; 9 while (I ++ <5) // broadcast data 5 times and end 10 {11 Console. writeLine ("I Am a broadcast thread {0}. I am broadcasting! ", Thread. CurrentThread. ManagedThreadId); 12 Thread. Sleep (1000); 13} 14 Console. WriteLine (" I'm not playing! "); 15 tokenThread = null; 16}); 17 if (tokenThread = null) // if the token is not used, it is assigned to the thread, otherwise, this thread is skipped 18 {19 tokenThread = th; 20 th. start (); 21} 22 23}View Code

First, define a full thread token, assign NULL to it, and then write a Greeting () function. In the function, a broadcast thread is opened, which is broadcast for five times and takes 5 seconds, when the broadcast ends, the token is released and re-assigned to NULL. When the thread is started, an if (tokenThread = null) judgment is added. if the token is in the release status, the current thread obtains the token, starts the thread, and executes the broadcast five times. Otherwise, the request is skipped, which means that the Greeting () function has not been executed yet.

The main function is very simple, but a while loop is opened, and the Greeting () function is used once in less than 1 second.

1 static void Main (string [] args) 2 {3 while (true) 4 {5 Greating (); 6 Thread. Sleep (1000); 7} 8 9}View Code

If there is no token control, a broadcast thread will be opened in seconds.

The final result is as follows. A new thread broadcast will be started after five broadcasts:

 

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.