1. There can be multiple threads executing code concurrently in a process. Processes are relatively independent. One process cannot access the data of another process (unless distributed computing is used), and the failure of one process will not affect the operation of other processes. The Windows system uses processes to divide work into multiple an independent area.
2. Thread is the basic execution unit in a process and the basic unit of CPU time allocated by the operating system. A process can contain several threads. The first thread executed at the process entry is regarded as the main thread of the process. In .NET applications, the Main() method is used as the entry point. When this method is called, the system will automatically create a main thread. Advantages of multithreading:
Can complete multiple tasks at the same time;
Can make the program more responsive;
Tasks that take up a lot of processing time or tasks that are not currently processing can periodically give up their processing time to other tasks;
The task can be stopped at any time;
The priority of each task can be set to optimize program performance.
shortcoming:
Threads are also programs, so threads need to occupy memory. The more threads, the more memory they occupy.
Multithreading requires coordination and management, so it takes CPU time to keep track of threads.
Access to shared resources between threads affects each other, and contention for shared resources must be resolved.
Too many threads can lead to too complex control, which can eventually lead to many bugs.
In C#, threads are handled using the Thread class, which is in the System.Threading namespace. When using the Thread class to create a thread, you only need to provide the thread entry, which tells the program what to do with the thread. A thread is created by instantiating an object of the Thread class. When a new Thread object is created, a new managed thread is created. The Thread class receives a ThreadStart delegate or a constructor for the ParameterizedThreadStart delegate, which wraps the method called by the new thread when the Start method is called. The sample code is as follows:
Thread thread=new Thread(new ThreadStart(method));//Create a thread
thread.Start(); //Start the thread
The above code instantiates a Thread object, specifies the method method() to be called, and then starts the thread. The methods in the ThreadStart delegate as parameters do not need parameters and have no return value.
ParameterizedThreadStart delegates an object as a parameter, which can be used to easily pass parameters to the thread. The sample code is as follows:
Thread thread=new Thread(new ParameterizedThreadStart(method));//Create a thread
thread.Start(3); //Start the thread
Steps to create a multi-thread:
1. Write the method to be executed by the thread.
2. Instantiate the Thread class and pass in a delegate pointing to the method to be executed by the thread. (At this time, the thread has been generated, but not yet running)
3. Call the Start method of the Thread instance to mark that the thread can be executed by the CPU, but the specific execution time is determined by the CPU
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //Create a thread without parameters 6 Thread thread1 = new Thread(new ThreadStart(Thread1)); 7 //Call the Start method to execute the thread 8 thread1.Start(); 9 10 Console.ReadKey(); 11 } 12 13 /// <summary> 14 /// Create a method with no parameters 15 /// </summary> 16 static void Thread1() 17 { 18 Console.WriteLine("This is a method without parameters"); 19 } 20 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //Create a thread through ParameterizedThreadStart 6 Thread thread = new Thread(new ParameterizedThreadStart(Thread1)); 7 //Pass value to method 8 thread.Start("This is a delegate with parameters"); 9 Console.ReadKey(); 10 } 11 12 /// <summary> 13 /// Create a method with parameters 14 /// Note: The parameter type in the method must be of type Object 15 /// </summary> 16 /// <param name="obj"></param> 17 static void Thread1(object obj) 18 { 19 Console.WriteLine(obj); 20 } 21 }
property name | illustrate |
---|---|
CurrentContext | Get the current context in which the thread is executing. |
CurrentThread | Get the currently running thread. |
ExecutionContext | Gets an ExecutionContext object that contains information about the current thread's various contexts. |
IsAlive | Gets a value that indicates the execution state of the current thread. |
IsBackground | Gets or sets a value indicating whether a thread is a background thread. |
IsThreadPoolThread | Gets a value indicating whether the thread belongs to the managed thread pool. |
ManagedThreadId | Get the unique identifier of the current managed thread. |
Name | Gets or sets the name of the thread. |
Priority | Gets or sets a value indicating the thread's scheduling priority. |
ThreadState | Gets a value that contains the state of the current thread. |