A thread pool (ThreadPool class) can be used when there are many threads to be created and the threads are smaller ). ThreadPool is a static method that provides operations on a set of threads. It increases the number of threads when the number of threads is insufficient and releases resources when the number of Idle threads is too large.
1. Simple ThreadPool Application
Call the ThreadPool. QueueUserWorkItem () method, pass a WaitCallBack delegate type method, and assign this method to the thread in the thread pool. The thread pool runs automatically. If the thread pool is not running, a thread pool is created and the first thread is started. If the thread pool is already running and there is an idle thread to complete the task, the job is passed to the thread.
The following method is a method that complies with the WaitCallBack delegate:
1: static void WaitCallBackMethod(object param)
2: {
3: for (int i = 0; i < 5; i++)
4: {
5: Console.WriteLine(String.Format("Thread {0} is running", param));
6: Thread.Sleep(1000);
7: }
8: }
Then add the method to the thread pool in the main thread:
1: static void Main(string[] args)
2: {
3: for (int i = 1; i <= 3; i++)
4: {
5: ThreadPool.QueueUserWorkItem(WaitCallBackMethod, i);
6: }
7: Console.Read();
8: }
The second parameter of the QueueUserWorkItem () method is an object-type parameter, which can be passed into a thread. In the main thread, three methods are passed in to the thread pool. The running result is as follows:
We can see that the three threads are acting separately. The order is different because of the operating system scheduling.
2. maximum and minimum number of threads
The ThreadPool class will increase or decrease the number of threads in the pool as needed until the maximum number of threads is reached. The maximum number of threads in the pool is configurable. In a dual-core CPU, the default value is 1023 worker threads and 1000 I/O threads. You can also specify the minimum number of threads that should be started immediately when creating a thread pool, and the maximum number of threads available in the thread pool. If more jobs are to be processed and the number of threads in the thread pool reaches the limit, the latest jobs will be queued and the threads must wait for them to complete their tasks.
You can view the following methods:
1: static void MaxThreads()
2: {
3: int workerThreads;
4: int ioThreads;
5:
6: ThreadPool.GetMaxThreads(out workerThreads, out ioThreads);
7: Console.WriteLine(String.Format("Max worker threads: {0}; Max I/O threads: {1}", workerThreads, ioThreads));
8:
9: ThreadPool.GetMinThreads(out workerThreads, out ioThreads);
10: Console.WriteLine(String.Format("Min worker threads: {0}; Min I/O threads: {1}", workerThreads, ioThreads));
11: }
Result:
3. Thread Pool restrictions
- All threads in the thread pool are background threads. If all foreground threads of a process are finished, all background threads are stopped. You cannot change the thread into the pool to the foreground thread.
- The priority or name cannot be set for the thread that enters the pool.
- For COM objects, all threads in the pool are Multithreaded Apartment (MTA) threads. Many COM Objects Require single-threaded Apartment (MTA) threads.
- The threads in the pool can only be used for tasks with shorter time periods. If the Thread is always running (such as the Word Spelling Checker Thread), you should use the Thread class to create a Thread.
References: C # Advanced Programming
Http://www.cnblogs.com/sosowjb/archive/2012/08/11/2633793.html