C # self-implemented thread pool function (2)

Source: Internet
Author: User

C # self-implemented thread pool function (2)
Introduction

In the previous c # self-implemented thread pool function (1), we basically implemented a program that can run, rather than actually called a thread pool. Because the code in the previous article has a fatal bug, that is, no task is not waiting, but a while loop is carried out frantically, and the linked list of tasks is locked, the cause of this problem is that the performance is quite low, and the program reflection speed is very slow (when a new task is added, it takes a long time for the job to start running.

To solve this problem, we need to use some method to make the program synchronize the process.

Method 1

Use semaphores

In order to reduce the lock operation on the task, the test is performed only when the task is not empty. Our semaphores represent the number in the task table. When s. WaitOne (); is successful, we start to lock and retrieve the task.

While (flag & TaskQueue! = Null) {// wait for the task ThreadPoolManager. s. waitOne (); // get the 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.

// A public int MaxJobNum = 1000; public static Semaphore s needs to be defined for semaphores;

And initialize the semaphores s = new Semaphore (0, MaxJobNum) When initializing this class );

This enables synchronization.

Below is a test class

 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);        }



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPGgxPre9t6i2/unzip vade + Cs7Sw8eyu8rH08PQxbrFwb + rotate/rotate = "brush: java;"> public void run () {while (flag & Ta SkQueue! = Null) {// wait for the task // ThreadPoolManager. sep. waitOne (); // wait for the task while (TaskQueue. count = 0 & flag) {try {ThreadPoolManager. locks. waitOne () ;}catch (Exception) {}// gets the 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}It is blocked only when the number of task lists is 0, and continues until AddTask is executed.

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.