Home >Backend Development >C#.Net Tutorial >C# thread synchronization and thread pool brief analysis
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)!