C # Thread lecture series (3): Thread pool and file download server

Source: Internet
Author: User
Tags thread

If you design a server program that starts a thread whenever a user request is processed, it will consume the server's resources on a certain program. One of the best ways to do this is to create some thread objects in advance before the server starts, and then, when processing client requests, get the thread objects from the built threads and process the request. The structure of saving these thread objects is called the thread pool.

It can be implemented in C # by the System.Threading.ThreadPool class, by default, ThreadPool up to 500 worker threads and 1000 I/O threads (depending on the number of machine CPUs and the. NET Framework version) , these data may vary). Here is an example of using C # to get threads from a thread pool:

private static void execute (object state)
{
Console.WriteLine (state);
}
static void Main (string[] args)
{

int workerthreads;
int completionportthreads;
Threadpool.getmaxthreads (out of workerthreads, out completionportthreads);
Console.WriteLine (workerthreads);
Console.WriteLine (completionPortThreads);
ThreadPool.QueueUserWorkItem (Execute, "thread 1"); Gets a thread from the thread pool and runs the Execute
ThreadPool.QueueUserWorkItem (Execute, "Thread 2");
ThreadPool.QueueUserWorkItem (Execute, "thread 3");
Console.ReadLine ();
}

The following figure shows the results of the above code.

Note that threads obtained using ThreadPool are background threads.

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.