Thread family 06, viewing the thread pool and its threads through CLR code

Source: Internet
Author: User


In "Thread series 04, passing data to threads, thread naming, thread exception handling, Threads Pool", we already know that each process has a thread pool. Delegates can interact with the thread pool through Tpl,threadpool.queueuserworkitem. This experience: Observe the thread pool and its threads by looking at the CLR code.

-View thread pools and threads by encoding

Using the static method of ThreadPool QueueUserWorkItem put threads into the thread pool to see how the thread pool threads and the main program threads are performing.

    class Program
    {
        Static void Main (string[] args)
        {
            Console.WriteLine (" main thread start ");
             for (int i = 0; i < 5; i++)
            {
                ThreadPool.QueueUserWorkItem (SayHello, i);
            }
            Console.WriteLine (" Main thread End ");
        }
        Static void SayHello (object Arg)
        {
            int n = (int) arg;
            Console.WriteLine (" thread {0} received parameter: {1}, whether it is a background thread: {2}",
                Thread.CurrentThread.ManagedThreadId,
                N
                Thread.CurrentThread.IsBackground);
        }       
    }

0 threads that join the thread pool using the QueueUserWorkItem method are background threads
0 once the main thread ends, the background thread ends
0 in the main program for statement block, there are 2 threads that have been created and executed

Let both the main thread and thread pool threads sleep for a while.

   class Program
    {
        Static New Random ();
        Static void Main (string[] args)
        {
            Console.WriteLine (" main thread start ");
             for (int i = 0; i < 5; i++)
            {
                ThreadPool.QueueUserWorkItem (SayHello, i);
            }
            Thread.Sleep (R.next (250, 500));
            Console.WriteLine (" Main thread End ");
        }
        Static void SayHello (object Arg)
        {
            Thread.Sleep (R.next (250, 500));
            int n = (int) arg;
            Console.WriteLine (" thread {0} received parameter: {1}, whether it is a background thread: {2}",
                Thread.CurrentThread.ManagedThreadId,
                N
                Thread.CurrentThread.IsBackground);
        }       
    }

0 threads in the thread pool are still background threads
0 after the main thread has been sleep for a period of time, thread pool threads are executed before the end of the main thread

Let the main thread sleep for a longer period of time.

        Static void Main (string[] args)
        {
            Console.WriteLine (" main thread start ");
             for (int i = 0; i < 5; i++)
            {
                ThreadPool.QueueUserWorkItem (SayHello, i);
            }
            Thread.Sleep (R.next (1000, 3000));
            Console.WriteLine (" Main thread End ");
        }

When the main thread takes longer to sleep, the thread pool has more threads being executed.

-Use "Immediate window" to view thread pools and threads in the CLR

Above, it is too inconvenient to look at thread pools and threads by code. We can see the thread pools and threads in the CLR with the SOS debugging extension in the Immediate window.

→ Break Breakpoints on "Thread.Sleep (R.next (1000, 3000))"
→ Click on "Start"
→ When the code runs to the breakpoint, click "Debug"--"window"--"instant", open "Immediate Window"
→ Enter the following command to load the SOS debug extension

For the use of the SOS Debugging extension, refer to Debugging view CLR run code.
→ Enter the following command to view the thread pool

0Worker thread shows the status of threads in the current thread pool
0Work Request in queue displays the threads in the current queue
→ Enter the following command to view all threads

0MTA (Finalizer) represents a thread that is recycled by GC
0MTA (Threadpool worker) represents a thread in the thread pool

-View all thread types on the CLR managed heap

→ Enter the following command to view all the thread types on the CLR managed heap

-View the CLR managed heap Live Pool instance

→ Locate system.threadpoolworkqueue and copy the Mt code in front of it
→ Enter the following command to view the heap address, quantity, etc. of the thread pool instance

-View CLR managed thread pool addresses

→ Copy System.Threading.ThreadPoolWorkQueue's managed heap address
→ Enter the following command to view the CLR managed thread pool address contents

The Thread family includes:

Thread series 01, foreground thread, background thread, thread synchronization thread series 02, multiple threads simultaneously handle a lengthy task to save time thread series 03, multithreading shared data, multithreading does not share data

Thread Family 04, passing data to thread, thread naming, threading exception handling, thread pool thread family 05, manually ending thread thread family 06, viewing thread pool and its threads through CLR code

Thread family 06, viewing the thread pool and its threads through CLR code

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.