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:
- WithTask Parallel library (Framework 4.0)
- CallThreadpool. queueuserworkitem
- Use asynchronous delegation.
- WithBackgroundworker..
Some of the following structures indirectly use the thread pool:
- WCF, remoting, Asp.net, and asmx Web ServicesApplicationProgram.
-
- System. Timers. TimerAndSystem. Threading. Timer.
-
- FrameworkFor exampleWebClientClass, and mostBeginxxxMethod.
- Plinq
Some problems with using the thread pool:
- You cannot set the name of a thread pool thread.
- All thread pool threads are background threads.
- 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