C# Thread synchronization and thread pool
The example is very simple, prepare 5 threads, each thread outputs numbers to the console at the same time, and then observe the output results .
Code description:
////Thread list
private static List<Thread> _threadList; static voidMain(string[] args) { Program._threadList= new List<Thread>(); ////附加5个线程 for (inti = 0; i < 5; i++) { Program.AppendThread(); } ////开始执行所有测试线程 Program.ExecuteThread(); ////按任意键退出 Console.ReadLine(); } /// <summary> /// 将新的测试线程附加到测试线程列表,线程执行逻辑就是输出10个数字 /// 注意初始化的时候设置为后台线程了,这样可以保证主线程退出的时候其他线/// 程自动退出 /// </summary> public staticvoid AppendThread() { Program._threadList.Add(newThread(new ThreadStart( () => { for (int i = 0; i < 10; i++) { Console.WriteLine(i); } })){ IsBackground = true }); } /// <summary> /// 开始执行所有测试线程 /// </summary> public staticvoid ExecuteThread() { foreach(Thread t in _threadList) { t.Start(); } }
Observe the execution results, we can see the results as follows:
According to the result (the output of numbers is irregular), it can be seen that interference has occurred between threads. The strategy is to add a synchronization member for thread synchronization:
/// <summary> /// 多线程同步的对象 /// </summary> private static object _syncObj = new object(); 另外,在线程执行的地方加锁: Program._threadList.Add(newThread(new ThreadStart( () => { lock (_syncObj) { for (int i = 0; i < 10;i++) { Console.WriteLine(i); } } })) { IsBackground = true });
Observation results:
You can see that through the Lock keyword, locking a multi-thread synchronized variable can indeed synchronize threads.
Now look at the second way:
Use the monitor keyword for synchronization, code:
Monitor.Enter(_syncObj); try { for (int i = 0; i < 10; i++) { Console.WriteLine(i); } } finally { Monitor.Exit(_syncObj); }
Check the results, you will find that the thread has been synchronized.
The third way:
Now let us refactor the code, create a new ThreadManager class, and move all the responsibilities of the class into it:
class ThreadManager { /// <summary> /// 线程列表 /// </summary> private staticList<Thread> _threadList; staticThreadManager() { _threadList = new List<Thread>(); } /// <summary> /// 附加新线程 /// </summary> public staticvoid AppendThread() { ThreadManager._threadList.Add(newThread(new ThreadStart( () => { for (int i = 0; i < 10; i++) { Console.WriteLine(i); } })){ IsBackground = true }); } /// <summary> /// 开始执行所有线程 /// </summary> public staticvoid ExecuteThread() { foreach(Thread t in _threadList) { t.Start(); } } }
Make corresponding changes to the code called by the Main function:
static voidMain(string[] args) { ////附加5个线程 for (int i = 0; i < 5; i++) { ThreadManager.AppendThread(); } ////开始测试 ThreadManager.ExecuteThread(); ////按任意键继续 Console.ReadLine(); }
Since no processing is done for thread synchronization, the result can definitely be guessed that the thread is asynchronous:
Now add features to the ThreadManager class: [Synchronization], and run again Finally, it is found that the threads are synchronized. This is the fourth solution for thread synchronization. It is very simple to use, but first it requires that the execution logic be placed in a class, because it can ensure that all methods in this class are Thread-safe, so its performance is relatively inefficient.
Is there any way to synchronize threads? The answer is yes, that is the fourth method-thread pool.
Now let’s take a look at how to use the thread pool to implement it:
static void Main(string[]args) { /////定义一个waitCallback对象,并定义它的行为,就是向控制台输出十个数字同时可以传递/////一个参数(这个参数是可选的) WaitCallback work = new WaitCallback((o)=> { for(int i = 0; i < 10; i++) { Console.WriteLine(i); } }); ////执行5次 for (inti = 0; i < 5; i++) { /////如果这里需要传递参数,可以调用另一个重载方法 ThreadPool.QueueUserWorkItem(work); } ////按任意键继续 Console.ReadLine(); }
Does this complete the logic just now? Yes, we can see the results after running it, the threads are synchronized.
What other benefits does multi-threading bring?
The thread pool reduces the number of thread creation, starts and stops, thereby improving efficiency;
Using the thread pool allows us to Focus on business logic rather than multi-threaded architecture (however, manual thread management should be preferred in some cases)
If you need a foreground thread or set a priority level, or a thread pool The thread in is always a background thread, and its priority is the default;
If you need a thread with a fixed identity to facilitate exiting, suspending or discovering it by name.
The above is the brief analysis of C# thread synchronization and thread pool. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.


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

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool