What is a process?
When a program starts running, it is a process that includes the memory and system resources that are used by the programs and programs that are running.
And a process is made up of multiple threads.
What is a thread?
A thread is a flow of execution in a program, each with its own proprietary register (stack pointers, program counters, and so on), but the code area is shared, meaning that different threads can execute the same function.
What is multithreading?
Multithreading refers to a program that contains multiple execution streams, in which multiple different threads can be run concurrently to perform different tasks, that is, allowing a single program to create multiple threads of parallel execution to accomplish their tasks.
Benefits of Multithreading:
can improve CPU utilization. In multithreaded programs, when a thread has to wait, the CPU can run other threads instead of waiting, which greatly increases the efficiency of the program.
The disadvantage of Multithreading:
Threads are also programs, so threads need to consume memory, and more threads consume more memory;
Multithreading requires coordination and management, so CPU time is required to track threads;
Access to shared resources between threads affects each other, and the issue of competing shared resources must be resolved;
Too many threads can lead to too much control and can eventually cause many bugs;
Next, the multithreading mechanism in C # programming is explored. In order to avoid the tedious steps of creating a GUI, and to more clearly approximate the nature of the thread, all the next programs are console programs, and the final console.readline () of the program is to stop the program halfway in order to see the output during execution.
There is at least one main thread for any program to execute.
A visual impression of the threading example:
SystemThread.cs
Using System;
Using System.Threading;
Namespace ThreadTest
{
Class Runit
{
[STAThread]
static void Main (string[] args)
{
Thread.currentthread.name= "System thread";//named "System thread" for the current thread
Console.WriteLine (thread.currentthread.name+ "' Status:" +thread.currentthread.threadstate);
Console.ReadLine ();
}
}
}
The output is as follows:
System Thread ' s status:running
Here, we get the currently executing thread through the static property of the thread class, assign the "System Thread" to its Name property, and finally output its current state (ThreadState) CurrentThread.
A static property is a property that is common to all objects of this class, no matter how many instances of the class you create, but the static properties of the class are only one in memory. It is easy to understand why CurrentThread is static--although multiple threads exist at the same time, at some point the CPU can only execute one of them.
At the head of the program, we use the following namespaces:
Using System;
Using System.Threading;
In the. NET Framework class library, all classes related to multithreaded application are placed in the System.Threading namespace. If you want to use multi-threading in your application, you must include this class.
We create and control threads through the thread class provided, the ThreadPool class is used to manage the thread pool, and so on.
(It also provides a mechanism for solving practical problems such as thread execution scheduling, deadlock, and inter-thread communication.) )
The thread class has several critical methods, which are described below:
Start (): Start thread;
Sleep (int): Static method that pauses the specified number of milliseconds for the current thread;
Abort (): This method is usually used to terminate a thread;
Suspend (): The method does not terminate the unfinished thread, it simply suspends the thread and can be resumed later;
Resume (): Restores the execution of the thread that was suspended by the suspend () method;
C # multithreaded Learning series:
C # multithreaded Learning (ii) How to manipulate a thread
Http://www.cnblogs.com/xugang/archive/2008/04/06/1138841.html
C # multithreaded Learning (iii) producers and consumers
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118594.html
C # Multi-threaded Learning (four) multi-threaded automatic management (thread pool)
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118584.html
C # multithreaded Learning (v) multi-threaded automatic management (timers)
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118537.html
C # multithreaded Learning (vi) Mutex objects
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118530.html
Related articles:
dotnet Threading Problem Solving
Using threads in Visual C #
Summary of C # thread resource synchronization methods
C # Threading Series Lectures (1): BeginInvoke and EndInvoke methods
C # Multithreading (a) related concepts of multithreading (goto)