Introduction to the multi-thread Series 3: Thread Pool

Source: Internet
Author: User

Thread Pool:

Each thread is allocated by default.1 MBMemory, inC #These are actually allocated. When a thread starts, it takes several hundred microseconds to allocate a temporary stack.

The thread pool allows you to use threads more efficiently through looping.

 

The thread pool is like an outsourced labor team. If there are jobs for them, they will manage all the work of the workers. You don't need to spend time looking for a single labor, and then they will be fired after the job is completed,

For the labor team, after the labor team completes your task, it will return to its own team. The management of the labor team does not require you to take responsibility, because it is handled by the Labor team,

If there are too many tasks, the labor team will recruit one for itself. If it is not enough, it will continue to recruit, but if there are fewer tasks and there are more labor, I'm sorry, some workers will be dismissed by the labor management staff.

 

There are many ways to enter the thread pool:

    1. WithTask Parallel library (Framework 4.0)
    2. CallThreadpool. queueuserworkitem
    3. Use asynchronous delegation.
    4. WithBackgroundworker.. 

Some of the following structures indirectly use the thread pool:

  1. WCF, remoting, Asp.net, and asmx Web ServicesApplicationProgram.
  2. System. Timers. TimerAndSystem. Threading. Timer.
  3. FrameworkFor exampleWebClientClass, and mostBeginxxxMethod.
  4. Plinq 

Some problems with using the thread pool:

    1. You cannot set the name of a thread pool thread.
    2. All thread pool threads are background threads.
    3. Blocking a thread pool thread may trigger the creation of a new thread unless you callThreadpool. setminthreadsMethod.

PassThread. currentthread. isthreadpoolthreadAttribute to query whether a thread is a thread pool thread.

 

Threadpool

1: PassTaskUse thread pool:

Public   Static   Void Mainthread ()
{
Task < String > Task = Task. Factory. startnew < String >
(() => Downloadstring ( " Http://www.google.com " ));

// dosomething
string result = task. result;
}

static string downloadstring ( string URI)
{< br> using (var wc = New system. net. webClient ()
return WC. downloadstring (URI);
}

When querying task. result, the thread is blocked and waits for the task to return the result.

 

2:PassThreadpool. Queueuserworkitem

Public   Static   Void Mainthread ()
{
Threadpool. queueuserworkitem (GO );
Threadpool. queueuserworkitem (go, 123 );

Console. Readline ();
}

Static VoidGo (ObjectData)
{
Console. writeline ("Hello from the thread pool!" +Data );
}

Output:

Hello from the thread pool!

Hello from the thread pool! 123

 

3:With the help of delegatedBeginxxxMethod:

Public   Static   Void Mainthread ()
{
Func < String , Int > Method = Work;
Method. begininvoke ( " Test " , Done, method );
}

Static IntWork (StringS ){ReturnS. length ;}

Static   Void Done (iasyncresult cookie)
{
VaR target = (Func < String , Int > ) Cookie. asyncstate;
Int Result = Target. endinvoke (cookie );
Console. writeline ( " String Length is: "   + Result );
}

After the method is passed as a parameter, the passed method can be used in the asyncstate of the cookie,

Because the cookie. asyncstate type is object, the conversion is required and the endinvoke method is called to obtain the result.

 

References:

Http://www.albahari.com/threading/

CLR via C #3.0

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.