C # ThreadPool.QueueUserWorkItem Typical case detailed

Source: Internet
Author: User
Tags foreach sleep thread

Using System;

Using System.Collections.Generic;

Using System.Threading;

Namespace Threadpooltest

{

Class Program

{

static void Main (string[] args)

{

Declare a list of action delegates, add some delegate tests with

List actions = new List

{

() =>{console.writeline ("A-1");},

() =>{console.writeline ("A-2");},

() =>{console.writeline ("A-3");},

() =>{console.writeline ("A-4");}

};

Traversing output results

foreach (var action in actions)

{

ThreadPool.QueueUserWorkItem (State => Action (), NULL);

}

Console.readkey ();

}

}

}

This very unexpected result is as follows:

Why is that so?

I analyze, first of all, we all know that it takes time to create a thread, the thread pool is easy to manipulate as an object for managing threads, but it is difficult to control when it comes to detail, and the idea is that when the thread pool receives the request, a free thread from the thread pool is assigned to it and then started. But thread pool threads from creation to startup takes time, but the main thread's loop is obviously just instantaneous, it's completely unnecessary to be mixed in with the waiting of the thread pool, so the main thread completes the loop. The last thread pool received the request delegate is the last one in the list, how to solve?

Mode one: Thread.Sleep (1);

Bloggers say the way to Thread.Sleep (n) is difficult to accept, in fact, as long as the principle of careful analysis, you can know that this way is acceptable or even subtle, Thread.Sleep (n) is blocking the current thread for a certain time, The processing of the thread in either the primary thread or the thread pool is effective, at least for the time it took for the thread in the thread pool to request and start. Of course, the thread blocking here must be the main line, so that it does not get CPU time, so it succeeds in getting the expected results.

Then another way was taken,

Mode two:

foreach (var action in actions)

{

var tempaction = action;

ThreadPool.QueueUserWorkItem (State => tempaction (), NULL);

}

Store the variables in the loop in a temporary variable, why do you do that?

Everyone knows that the thread pool has a request for it to assign a free thread to work with it, when the variable in each cycle is different is the object is not at the same time each request differences, of course, the thread pool must be prepared for each, unlike the loop variable directly as a request, At this point each request is ready in sequence to start up in turn. Of course, it was successful.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.