1024programmer Asp.Net C# multithreading learning (1) related concepts of multithreading

C# multithreading learning (1) related concepts of multithreading

C# multithreading learning (1) related concepts of multithreading

C# Multithreading Learning (1) Multithreading Related Concepts

What is a process?

When a program starts running, it is a process, and the process includes the running program and the memory and system resources used by the program.

A process is composed of multiple threads.

What is a thread?

A thread is an execution flow in a program, and each thread has its own dedicated registers (stack pointer, program counter, etc.),

But the code area is shared, that is, different threads can execute the same function.

What is multithreading?

Multi-threading means that the program contains multiple execution streams, that is, a program can run multiple different threads at the same time for execution

Different tasks, that is to say, allow a single program to create multiple parallel execution threads to complete their respective tasks.

The benefits of multithreading:

Can improve CPU utilization. In a multithreaded program, when one thread must wait, the CPU can run other threads.

thread instead of waiting, which greatly improves the efficiency of the program.

The downside of multithreading:

Threads are also programs, so threads need to occupy memory, and more threads occupy more memory;

Multithreading requires coordination and management, so CPU time is required to track threads;

The access to shared resources between threads will affect each other, and the problem of competing for shared resources must be solved;

Too many threads will lead to too complicated control, which may eventually cause many bugs;

Next, we will discuss the multithreading mechanism in C# programming. In order to save the cumbersome steps of creating GUI and approach the essence of threads more clearly, all the following programs are console programs, and the last Console.ReadLine() of the program is to stop the program halfway. down to see the output during execution.

When any program is executing, there is at least one main thread.
An intuitive thread example:

//SystemThread.cs
 using System;
 using System. Threading;
 namespace ThreadTest
 {
 class RunIt
 {
 [STAThread]
 static void Main(string[] args)
 {
 Thread.CurrentThread.Name="System Thread";//Name the current thread "System Thread"
 Console.WriteLine(Thread.CurrentThread.Name+"Status:"+Thread.CurrentThread.ThreadState);
 Console. ReadLine();
 }
 }
 }
 

The output is as follows:

System Thread's Status: Running
 

Here, we obtain the currently executing thread through the static property CurrentThread of the Thread class, assign a value to its Name property “System Thread”, and finally output it The current state of the thread (ThreadState).
The so-called static attributes are the attributes common to all objects of this class. No matter how many instances of this class you create, there is only one static attribute of the class in memory. It is easy to understand why CurrentThread is static – although there are multiple threads existing at the same time, but at a certain moment, the CPU can only execute one of them.
At the head of the program, we use the following namespace:
using System;
using System.Threading;
In .net framework class library, all classes related to multi-threading mechanism application are placed in System.Threading namespace. This class must be included if you want to use multithreading in your application.
We use the Thread class provided to create and control threads, and the ThreadPool class is used to manage thread pools, etc. (In addition, it also provides a mechanism to solve practical problems such as thread execution arrangement, deadlock, and inter-thread communication.)

Thread class has several crucial methods described as follows:

Start(): start the thread;
Sleep(int): static method, suspend the current thread for the specified number of milliseconds;
Abort():Usually use this method to terminate a thread;
Suspend(): This method does not terminate the unfinished thread, it only suspends the thread and can resume it later;
Resume():Resume the execution of the thread suspended by the Suspend() method;

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/c-multithreading-learning-1-related-concepts-of-multithreading/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索