Asynchronous programming, c # asynchronous programming
I. threads
1.1 main thread
. Net uses the main () method as the entry point of the program. When this method is called, the main thread is created.
1.2 worker threads
A thread created by the main thread is called a worker thread to execute a specific task.
1.3 foreground thread
By default, all threads created by Thread. Start () are foreground threads. Foreground threads can stop the end of an application. That is, after all foreground threads are executed, CLR * can close the application.
Foreground thread is a worker thread
1.4 background thread
The background thread does not affect the end of the application. After all foreground threads are executed, the application is closed no matter whether or not the background thread is executed.
Background threads are generally used to process unimportant tasks (such as checking mailboxes at intervals ). The background thread belongs to the worker thread.
1 static void Main (string [] args) 2 {3 Console. writeLine ("main Thread start"); 4 Thread t1 = new Thread (Task1); 5 t1.Start (); 6 7 Thread t2 = new Thread (Task2); 8 t2.IsBackground = true; // background Thread 9 t2.Start (); 10} 11 12 public static void Task1 () 13 {14 Thread. sleep (1000); 15 Console. writeLine ("foreground Thread start"); 16} 17 18 public static void Task2 () 19 {20 Thread. sleep (3000); 21 Console. writeLine ("background thread start"); 22}View Code
The preceding code execution result is shown in. It can be seen that the application is closed after the foreground thread is executed. No "background thread start" is output, and the background thread is not executed.
1 static void Main (string [] args) 2 {3 Console. writeLine ("main Thread start"); 4 Thread t1 = new Thread (Task1); 5 t1.Start (); 6 7 Thread t2 = new Thread (Task2 ); 8 // t2.IsBackground = true; // background Thread 9 t2.Start (); 10} 11 12 public static void Task1 () 13 {14 Thread. sleep (1000); 15 Console. writeLine ("foreground Thread start"); 16} 17 18 public static void Task2 () 19 {20 Thread. sleep (3000); 21 Console. writeLine ("background thread start"); 22}View Code
As shown in the preceding code running result, both threads are foreground threads. After all the two threads are executed, the application is closed.
Ii. Thread Pool
The thread pool is designed for a sudden explosion of threads. By using a limited number of fixed threads for a large number of operation services, the thread creation and destruction time are reduced, thus improving efficiency.
ThreadPool is used in scenarios where concurrent running of several tasks is not long and does not affect each other. The thread created by ThreadPool is the background thread.
1 static void Main (string [] args) 2 {3 Console. WriteLine ("the Main thread starts! "); 4 // create the task to be executed 5 WaitCallback workItem = state => Console. writeLine ("current Thread Id:" + Thread. currentThread. managedThreadId); 6 // call 10 times repeatedly 7 for (int I = 0; I <10; I ++) 8 {9 ThreadPool. queueUserWorkItem (workItem); 10} 11 Console. readLine (); 12}View Code
The preceding code execution result is shown in. It can be seen that not every execution task creates a new thread, but it is a thread maintained in the loop utilization thread pool. If the last sentence is removed, Console. ReadLine () will be returned directly after only "starting the main program" is output. We can see that the thread created in the thread pool is the background thread.
Ongoing maintenance .......
CLR:
Differences between threads and tasks
What is the role of async await?