1: Threads and processes
For all operations that need to wait, such as moving files, database and network access, it takes a certain time. At this time, a new thread can be started to perform these time-consuming operations. Multiple threads of a process can run on different CPUs or different cores of multi-core CPUs at the same time. A process contains at least one thread. When an application starts, it usually starts a process, and then the process starts multiple threads
Foreground Thread and background Thread: as long as any foreground process is running, the process of the application will be running. By default, the threads created with Thread are foreground threads, and the threads created with Thread pool are background threads. The threads created with Thread can be set through Thread.IsBackground, When all foreground threads are running, if there are still background threads running, all background threads will be terminated
2: Thread startup -- through asynchronous delegation
using System; class MainClass { public static void Main(string[] args) { Action test1_del = Test1; //test1_del.BeginInvoke(null, null); Action<int> test2_del = Test2; //test2_del.BeginInvoke(11, null, null); Func<int> test3_del = Test3; //1. Judge whether the thread has completed execution through the while loop //IAsyncResult test3_result = test3_del.BeginInvoke(null, null); //while (!test3_result.IsCompleted) //{ // Console.WriteLine("please wait....."); //} //Console.WriteLine(test3_del.EndInvoke(test3_result));// Get the return value of the asynchronous thread //2. Set the timeout by opening the wait handle. If it is greater than the timeout and the execution is not completed, false is returned //IAsyncResult test3_result = test3_del.BeginInvoke(null, null); //bool isEnd = test3_result.AsyncWaitHandle.WaitOne(1000); //3. Judge whether the thread has been executed through the callback function (recommended) IAsyncResult test3_result = test3_del.BeginInvoke((async) => { Console.WriteLine(test3_del.EndInvoke(async)); }, null); Console.WriteLine("this is main"); Console.ReadKey(); } static void Test1() { Console.WriteLine("this is test1"); } static void Test2(int a) { Console.WriteLine("this is test2"); } static int Test3() { Console.WriteLine("this is test3"); return 11; } }
2: Thread start - through thread
A CPU can only execute one Thread at a time. When many threads need to be executed, the Thread scheduler will judge which Thread to execute first according to the Priority. If the Priority is the same, it will execute one by one. You can set the Priority through the Priority attribute in the Thread class
using System; using System.Threading; class MainClass { public static void Main(string[] args) { Thread test1_thread = new Thread(Test1); test1_thread.Start(); Thread test2_thread = new Thread(Test2); test2_thread.Start(1); //A Thread with a return value cannot be opened through the Thread class //Thread test3_thread = new Thread(Test3); //test3_thread.Start(1); Console.WriteLine("this is main"); Console.ReadKey(); } static void Test1() { Console.WriteLine("this is test1"); } //When using Thread to start multithreading with parameters, the parameters must be of object type and there can be only one parameter. Pass the parameters in the start method of starting the Thread static void Test2(object o) { Console.WriteLine("this is test2"); } static int Test3() { Console.WriteLine("this is test3"); return 11; } }
When a thread is created, the status of the thread is Unstarted. When the thread is started with Start, the status of the thread is still Unstarted and will not immediately change to Running status, because the thread scheduler will select the threads to be executed in order. When this thread is selected, the status will be changed to Running status
3: Thread startup -- through thread pool
An application can only have one thread pool. The thread pool is designed to create a large number of threads in a certain period of time in order to improve efficiency. If the time of a thread is very long, thread pool is not recommended
using System; using System.Threading; class MainClass { public static void Main(string[] args) { ThreadPool.QueueUserWorkItem(Test1); Console.WriteLine("this is main"); Console.ReadKey(); } static void Test1(object o) { Console.WriteLine("this is test1"); } }
4: Thread startup - through Task
You can use ContinueWith in the Task class to set continuous tasks, that is, after executing one thread, you can execute the next thread
using System; using System.Threading.Tasks; class MainClass { public static void Main(string[] args) { //1. Start the thread with start Task test1_task = new Task(Test1); test1_task.Start(); //2. Start the thread through TaskFactory TaskFactory test1_taskFactory = new TaskFactory(); Task task = test1_taskFactory.StartNew(Test1); Task test2_task = new Task(Test2, 1); test2_task.Start(); //A thread with a return value cannot be opened through the Task class //Task test3_task = new Task(Test3); //test3_task.Start(); Console.WriteLine("this is main"); Console.ReadKey(); } static void Test1() { Console.WriteLine("this is test1"); } //When using Task to start multithreading with parameters, the parameters must be of object type and there can be only one parameter. Pass the parameters in the start method of starting the thread static void Test2(object o) { Console.WriteLine("this is test2"); } static int Test3() { Console.WriteLine("this is test3"); return 11; } }
5: Other operations of the thread
//Get current thread id Thread.CurrentThread.ManagedThreadId //Pause thread Thread.Sleep(1000);
6: Multithreaded lock
using System; using System.Threading; class MainClass { static Object locker = new Object(); private static int state = 1; static void Test() { while (true) { lock (locker)//Add mutex { state++; if (state == 1) { Console.WriteLine("equal 1"); } state = 1; } } } static void Main(string[] args) { Thread t1 = new Thread(Test); t1.Start(); Thread t2 = new Thread(Test); t2.Start(); } }