C # itself implements thread pool function (ii)

Source: Internet
Author: User
Tags semaphore

Introduced

In the previous C # implementation of the thread pool function (a), we basically implemented a program that can run, and not really called the thread pool. Because the code in the previous article has a fatal bug that is that there is no task is not waiting, but crazy to Do While loop, and try to lock task linked list, this brings the problem is the performance is very low, the program reflects very slow (after joining a new task, It will take a long time for the job to start running) and the reason for this is what we just said.

In order to solve this problem we need to use some method to enable the program to synchronize the process.

Method One

Using semaphores

In order to reduce the lock operation on task tasks, we are tempted only when the task is not empty. Our signal volume represents the number of tasks in the table, when S. WaitOne (); We started to lock up and take out the mission after the success.

while (flag && taskqueue! = null)            {                //wait for Task                threadpoolmanager.s.waitone ();                Get Task                Lock (Taskqueue)                {                    try                    {                        if (Taskqueue.count > 0)                            task = Taskqueue.dequeue ();                        else                            task = null;                    }                    catch (Exception)                    {                        task = null;                    }                    if (task = = null)                        continue;

Add two variables to the Threadpoolmanager class

Since the use of semaphores requires the definition of a public        int maxjobnum = +;        public static Semaphore s;


And initialize the semaphore when initializing this class S = new Semaphore (0, Maxjobnum);


This allows for synchronization

A test class is given below

static void Main (string[] args)       {           Threadpoolmanager TPM = new Threadpoolmanager (2);            Testtask T1 = new Testtask ("Task1");           Testtask t2 = new Testtask ("Task2");           testtask t3 = new Testtask ("Task3");           testtask T4 = new Testtask ("Task4");           Testtask T5 = new Testtask ("Task5");           Testtask T6 = new Testtask ("Task6");           Testtask t7 = new Testtask ("Task7");           Testtask T8 = new Testtask ("Task8");           Testtask T9 = new Testtask ("TASK9");            Tpm. AddTask (t1);           Tpm. AddTask (T2);           Tpm. AddTask (T3);           Tpm. AddTask (T4);           Tpm. AddTask (T5);           Tpm. AddTask (T6);           Tpm. AddTask (T7);           Tpm. AddTask (T8);           Tpm. AddTask (T9);       }



< yo "/kf/ware/vc/" target= "_blank" class= "Keylink" >vcd4kpggxpre9t6i2/jwvade+cs7sw8eyu8rh08pqxbrfwb+ jrm7sw8fkudpdqxv0b1jlc2v0rxzlbntatmq1z9bnrlk9cjxicj4kcrxa0ruyvaos1npuahjlywrqb29stwfuywdlcrp1yry7r8qxuvk0tl2o0ru49iagbg9j a3mgpsbuzxcgqxv0b1jlc2v0rxzlbnqozmfsc2upowq1sufkzfrhc2u1xmqxuvjsb2nrcy5tzxqoktvnqnaqtci0/ bxestnx96gjcsi7uvpo0sphttrxb3jrvghyzwfktcrydw66r8r91/bsu7j20khqobxe0n64xao8chjlignsyxnzpq== "Brush:java;" > public void Run () {while (flag && taskqueue! = null) {//Wait for Task//threadpoolmanager.sep.waitone ();//wait for task Whil E (Taskqueue.count = = 0 && flag) {try {ThreadPoolManager.locks.WaitOne ();} catch (Exception) {}}//Get task Lock (Taskqueue) {try {task = Taskqueue.dequeue ();} catch (Exception) {task = null;} if (task = = null) continue;} try {task. SetEnd (FALSE); Task. StartTask (); } catch (Exception) {} try {if (!task. Isend ()) {task. SetEnd (FALSE); Task. Endtask (); }} catch (Exception) {}}//end of While} only when the number of task lists is 0 o'clock we block until AddTask continues.

The above is C # itself to implement the thread pool function (ii) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.