Thread Pool (excerpt from C # Advanced Programming Version 7)

1, demand background
  Create a thread takes time. If there are different small task is completed, you can create many threads in advance, at the request should complete these tasks. This increases the number of threads is best when you need more threads, reducing the time required to release resources.
 
2, the thread pool appearances
  No need to create such a list himself. This list is hosted by the ThreadPool class. This class will increase or decrease the number of threads in the thread pool when needed, up to the maximum number of threads. The maximum number of threads in the pool is configurable.
  In the dual-core CPU, the default setting is 1023 worker threads and 1000 I / O thread. Quad-core CPU, the default setting is 2047 worker threads and 1000 I / O thread.
  You can also specify the minimum number of threads in the thread pool should be created to start immediately, and the maximum number of threads in the thread pool available.
  If there are more jobs to be processed, the number of thread pool thread to the limit, new jobs will queue and must wait for the thread to complete its task.
 
3. For chestnuts
  The following Examples I first read operation and the maximum number of threads the thread / O thread, writes the information to the console, and then in a for loop, the ThreadPool.QueueUserWorkItem call () method, passing a WaitCallBack delegate type, the JobForAThread ( ) method for imparting thread pool thread. After the thread pool receives this request, it will select a thread from the pool, to invoke the method.
  If the thread pool is not already running, it will create a thread pool and start the first thread. If the thread pool is already running, and there is a free thread to complete the task, the task passed to put this thread.
class CLRThread2
    {
        public static void ThreadMethod()
        {
            int nWorkThreads;
            int nCompletionPortThreads;
            ThreadPool.GetMaxThreads(out nWorkThreads, out nCompletionPortThreads);
            Console.WriteLine("Max worker threads:{0},I/O completion threads:{1}", nWorkThreads, nCompletionPortThreads);
 
            for (int i = 0; i < 5; i++)
            {
                ThreadPool.QueueUserWorkItem(JobForAThread);
            }
            Thread.Sleep(3000);
 
        }
 
        static void JobForAThread(object state)
        {
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("loop {0},running inside pooled thread {1}", i, Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(50);
            }
        }
    }

 

 
 
4, using the thread pool limit
  Thread pool with sounds simple, but it has some limitations:
  • All threads in the pool are background threads. If all foreground threads of the process all over, all the background thread stops. You can not put into the foreground thread pool threads instead.
  • You can not set priorities or name to the pooled thread
  • For COM objects, all threads are pooled multithreaded unit (multitihreaded apartment, MTA) threads. Many objects require a single-threaded COM unit (single-threaded apartmeng, STA) thread
  • Into the pool of threads used only for a short time task. If the thread has to run (such as Word's spelling checker thread), you should use the Thread class to create a thread.

Guess you like

Origin www.cnblogs.com/schangxiang/p/11292949.html