


A preliminary study on the multi-threading mechanism of C# (1)
1. The concept of multi-threading
Windows is a multi-tasking system. If you are using Windows 2000 and above, you can view the programs and processes currently running on the system through the Task Manager. What is a process? When a program starts running, it is a process. The process refers to the running program and the memory and system resources used by the program. A process is composed of multiple threads. A thread is an execution flow in the program. Each thread has its own private register (stack pointer, program counter, etc.), but the code area is shared, that is, different Threads can execute the same function. Multithreading means that a program contains multiple execution streams, that is, multiple different threads can be run simultaneously in one program to perform different tasks, which means that a single program is allowed to create multiple parallel execution threads to complete their respective tasks. A good example of multithreading is the browser, where you can scroll pages while downloading Java applets or images, play animations and sounds when accessing new pages, print files, etc.
The benefit of multi-threading is that it can improve CPU utilization - no programmer wants his program to have nothing to do all the time. In a multi-threaded program, when one thread has to wait, the CPU can run other thread instead of waiting, which greatly improves the efficiency of the program.
However, we must also recognize the adverse aspects of threads themselves that may affect system performance in order to use threads correctly:
Threads are also programs, so threads need to occupy memory. The more threads, the more memory they occupy
Multiple threads require coordination and management, so CPU time is needed 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 cause the control to be too complex, which may eventually cause many bugs
Based on We can use a metaphor to deepen our understanding of the above understanding. Suppose there is a company with many employees who perform their own duties. Then we can think of this normally operating company as a process, and the employees in the company are threads. A company must have at least one employee. Similarly, a process must have at least one thread. In a company, you can have one employee do everything, but the efficiency is obviously not high, and it is impossible for a one-person company to grow; a program can also use only one thread to do things. In fact, some outdated This is true for languages such as fortune and basic, but like a one-person company, the efficiency is very low. If you make a large program, the efficiency is even lower - in fact, there is almost no single-threaded commercial software now. The more employees there are in the company, the more the boss has to pay them, and he has to spend a lot of energy to manage them and coordinate the conflicts and interests between them; the same is true for the program, the more threads, the more resources are consumed, requiring CPU time is used to track threads, and problems such as deadlock and synchronization must be solved. In short, if you don't want your company to be called a "skin bag company", you have to have a few more employees; if you don't want your program to look childish, introduce multi-threading into your program!
This article will discuss the multi-threading mechanism in C# programming, and solve problems such as thread control and communication between multi-threads through some examples. In order to save the tedious steps of creating a GUI and more clearly approach the nature of threads, all the following programs are console programs. The Console.ReadLine() at the end of the program is to stop the program midway so that you can clearly see the execution process. Output.
Okay, without further ado, let’s experience multi-threaded C#!
2. Manipulate a thread
When any program is executed, there is at least one main thread. The following small program can give readers an intuitive impression:
[CODE]
//SystemThread.cs
using System / /give current The thread is named "System Thread"
Console.WriteLine(Thread.CurrentThread.Name+"'Status:"+Thread.CurrentThread.ThreadState);
}
}
}
[/CODE]
What do you see after compiling and executing? Yes, the program will produce the following output:
System Thread's Status: Running
Here, we obtain the currently executing thread through the static property CurrentThread of the Thread class, and assign "System Thread" to its Name property. Finally, its current state (ThreadState) is output. The so-called static properties are properties common to all objects of this class. No matter how many instances of this class you create, there is only one static property of the class in the memory. It is easy to understand why CurrentThread is static - although multiple threads exist at the same time, the CPU can only execute one of them at a certain moment.
As demonstrated in the above program, we create and control threads through the Thread class. Notice that in the head of the program, we use the following namespace:
[CODE]
using System;
using System.Threading;
[/CODE]
In the .net framework class library, all Classes related to multi-threading mechanism applications are placed in the System.Threading namespace. It provides the Thread class for creating threads, the ThreadPool class for managing thread pools, etc. In addition, it also provides mechanisms to solve practical problems such as thread execution arrangements, deadlocks, and inter-thread communication. If you want to use multithreading in your application, you must include this class. The Thread class has several crucial methods, described as follows:
Start(): Start the thread
Sleep(int): Static method, pause the current thread for the specified number of milliseconds
Abort(): This method is usually used to terminate a Thread
Suspend(): This method does not terminate the unfinished thread, it only suspends the thread and can be resumed later.
Resume(): Resumes the execution of the thread suspended by the Suspend() method
The above is the content of the preliminary exploration of C#’s multi-threading mechanism (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
