C # study notes a thread: a thread basis

    This excerpt from the note: https://www.cnblogs.com/zhili/archive/2012/07/18/Thread.html , learning about the recording for subsequent lookup.

    Introduce a thread of

    Process (Process) is a collection of resource instances of an application to be used, each application runs in their process to ensure that the application is not affected by other applications.

    A thread is the basic process execution unit, a process can contain multiple threads. The first entry in the process of executing a thread is the main thread of a process, the .Net applications, are based on Main () method as a program entry (the process thread is the execution unit, the process is a container thread).

    Second, thread scheduling and priority  

    Windows is known as preemptive multithreading operating system, because the thread can be preempted at any time, and schedule another thread.

    Each thread is assigned a first of from 0 to 31 assigned priority, high-priority thread system to perform CPU.

    Windows 7 support relative thread priority: Idle, Lowest, Below Normal, Normal, Above Normal, Highest and Time-Critical. Normal is the default thread priority, however, can change the priority of the thread by setting the Thread Priority property in the program,

It is of type ThreadPriority enumerated types: Lowest, BelowNormal, Normal, AboveNormal and Highest, CLR and retain the Idle Time-Critical Priority for themselves.

    List of enumerated values ​​as follows:

Member name Explanation
Lowest Thread can be placed behind other priority thread.
BelowNormal Thread can be placed after the Normal priority thread before the Lowest priority threads.
Normal

Thread can be placed after AboveNormal priority thread before BelowNormal priority threads.

By default, the thread is placed in Normal priority.

AboveNormal Thread can be placed after the Highest priority thread before Normal priority threads.
Highest Thread can be placed before other priority threads.

    Third, the foreground thread and the background thread

    In .Net threads are divided into foreground and background thread thread:

    I, the main thread is executed when the program starts, if you need to create a thread, the thread that created the child thread the main thread, it is a foreground thread. 

    II, sub-thread can be a foreground thread can also be a background thread.

    III, foreground thread must all be executed, even if the main thread is closed off, then the process is still alive.

    IV, when all foreground thread to stop running, CLR will force the end of any background thread is still running, these background threads directly terminated, no exception is thrown.

    V, with the only difference foreground thread a background thread is a background thread does not block the process terminates, foreground thread can be modified to a background thread at any time.

        ///  <the Summary> 
        /// foreground thread and the background thread
         ///  </ the Summary> 
        Private  static  void ThreadType ()
        {
            // create a new thread (default is the foreground thread) 
            the Thread backThread = new new the Thread (Worker)
            {
                // Change the thread a background thread 
                IsBackground = to true
            };

            // start the thread through the Start method 
            backThread.Start ();

            // If backThread is a foreground thread, the application is terminated after five seconds.
            // If backThread a background thread, the application terminates immediately. 
            Console.WriteLine ( "apos Thread from main. " );
             // Console.Read (); 
        }

        private static void Worker()
        {
            // rest 5 seconds 
            the Thread.Sleep ( 5000 );
            Console.WriteLine("It's from worker thread.");
        }

    Provided you keep IsBackground = true; but want to continue with the Worker () method, you can call thread.Join () method to achieve. Join () method to ensure that the main thread (foreground thread) not run until the end of the asynchronous thread thread (background thread) runs.

        static void Main(string[] args)
        {
            ThreadTypeUseJoin();
        }

        ///  <the Summary> 
        /// foreground and background thread threads using Join () method
         /// Join () method to ensure that the main thread (foreground thread) not run until the end of the asynchronous thread thread (background thread) running
         // /  </ Summary> 
        Private  static  void ThreadTypeUseJoin ()
        {
            // create a new thread (default is the foreground thread) 
            the Thread backThread = new new the Thread (Worker)
            {
                // Change the thread a background thread 
                IsBackground = to true
            };

            // start the thread through the Start method 
            backThread.Start ();
            backThread.Join();

            Console.WriteLine("It's from main thread.");
            Console.Read();
        }

        private static void Worker()
        {
            // rest 5 seconds 
            the Thread.Sleep ( 5000 );
            Console.WriteLine("It's from worker thread.");
        }

    Results are as follows:

    Use four simple thread

    In fact, in the above description foreground and background thread when the thread has passed ThreadStart commissioned to create a thread, and this time has achieved a process of a multi-threaded.

    By following ParameterizedThreadStart to achieve multi-threaded approach delegate:

        static void Main(string[] args)
        {
            ThreadTypeUseParameterized();
        }

        ///  <the Summary> 
        /// foreground thread and the background thread (use ParameterizedThreadStart delegate to achieve multi-threaded)
         ///  </ the Summary> 
        Private  static  void ThreadTypeUseParameterized ()
        {
            // create a new thread (default is the foreground thread) 
            the Thread backThread = new new the Thread ( new new ParameterizedThreadStart (worker1));

            // start the thread by the Start method 
            backThread.Start ( 123 );

            // If backThread is a foreground thread, the application is terminated after five seconds.
            // If backThread a background thread, the application terminates immediately. 
            Console.WriteLine ( " It's the Thread from main. " );
        }

        private static void Worker1(object parameter)
        {
            // rest 5 seconds 
            the Thread.Sleep ( 5000 );
            Console.WriteLine(parameter+" is from worker1 thread.");
            Console.Read();
        }

    Results are as follows:

 

Guess you like

Origin www.cnblogs.com/atomy/p/11946506.html