In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the example analysis of. NET thread pool technology to achieve multi-task batch processing. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.
I. Introduction to multi-thread technology application scenarios
This issue also brings you to share the real business scenarios encountered by A Ben in his actual work. Please follow A Ben's perspective on how to implement multi-task batch processing based on SmartThreadPool thread pool technology of open source components.
Have you ever encountered how to quickly and efficiently process Job task lists, how to batch process orders through multithreading, how to group text messages through multithreading, how to upload pictures to remote servers or Cloud Virtual Machine in batches, how to increase the utilization rate of CPU by applications through multithreading, and so on?
If you have encountered similar business scenarios and feel troubled, then today you read A Ben's sharing class next time you encounter such difficult problems will no longer be confused, thoughts will suddenly open up. If you are interested in the content of this tutorial, then everyone will learn with A Ben!
No more nonsense, direct dry goods, we do not produce dry goods, we are only dry goods porters.
II. Explanation of the meaning of concept name
2.1 What are Thread and Thread Pool?
Thread: The smallest unit of Windows task scheduling. Thread is an execution flow in the program, each thread has its own proprietary register (stack pointer, program counter, etc.), but the code area is shared, that is, different threads can execute the same function, in an application, often need to use multiple threads to deal with different things, which can improve the efficiency of the program, but also will not make the main interface unresponsive.
Thread Pool (TheadPool): The basic idea is still an object pool idea, open up a memory space, which stores a large number of (not dead) threads, thread execution scheduling in the pool is handled by the thread pool manager. When there is a thread task, select one from the pool, and return the thread object to the pool after execution, which can avoid the performance overhead caused by repeatedly creating thread objects and save system resources.
2.2 Why do I need a thread pool?
If your code involves heavy use of threads, it may crash beyond the maximum number of threads in the system, and creating and destroying threads each time is resource-intensive. Since threads in the thread pool are ready and waiting to be assigned, the application can use them directly without creating a new thread. So using ThreadPool can help you improve code efficiency and manage your threads. The benefits of using ThreadPool are as follows:
Reduce context switches between threads. After a thread executes a certain time slice, the system will automatically switch the cpu to another thread. At this time, it is also necessary to save the context state of the previous thread and load the context state of the new thread. When there are a large number of threads in the program, each thread will be allocated less and less time slices, and it may occur that the thread needs to switch to another thread because of how many operations it has not processed, so frequent context switches between threads will cost a lot of cpu time.
Reduce memory footprint. The system takes about 1MB of memory space to create each physical thread, and many programs like to create multiple physical threads first and poll periodically to process their tasks, which consumes both thread context switching time and memory waste. These tasks may require only one thread to fulfill their requirements. If a task needs to be executed for a long period of time, the thread pool can also automatically add threads, and when idle, destroy threads and release occupied memory.
2.3 Why is SmartThreadPool strongly recommended instead of. Net's default ThreadPool?
. Net's default ThreadPool is a static class, so there's no way to create a new pool yourself. The default thread pool is tied to an AppDomain, and there is only one thread pool per AppDomain. If you execute a long-cycle task in the thread pool that keeps one of the threads occupied, it may affect the performance of other programs in the application domain. For example, if you execute a long-cycle task in the thread pool of Asp.Net, it affects the concurrency of requests (thread pools default to a maximum number of threads).
SmartThreadPool is a famous. Net thread pool project, based on. Net development, better than the thread pool built in. Net. As the name suggests, intelligent thread pools. NET than certainly comes with the thread pool has excelled. Otherwise, there's no need to create another one.
SmartThreadPool key features are summarized as follows:
Thread pool instances can be created.
The number of thread pool worker threads can be dynamically adjusted.
WorkItems can return information, and callers can wait for multiple or all WorkItems to finish executing.
WorkItem priority can be set.
Wait...
III. Schematic diagram of thread pool processing multitasking
IV. Knowledge points involved in coverage
4.1 WinForm desktop application development small techniques and skills. as follows:
1. For example, System.Threading.Timer timer runs flexibly in a large number of actual projects.
2, how to quickly through the control library provided by A Ben to achieve desktop application UI beautification and humanized layout.
3. Use Settings.settings in C#to store user-defined profiles.
C#How to quickly create Windows service applications.
5. How C#supports batch files (bat) and how to connect remote servers through batch instructions to establish local mapping disks.
Wait...
4.2 This sharing class will bring you three technical solutions in actual projects. How to use multi-thread technology to realize batch processing of multi-tasks. When you encounter such common business scenarios in the future, you will feel "easy to get, handy." as follows:
1. How to use SmartThreadPool technology to achieve multitask batch processing. (Highly recommended)
2. How to use the default ThreadPool technology to achieve multitask batch processing.
3.How to use. NET 4.5 asynchronous programming Task technology to achieve multi-task batch processing.
4.3 Basic use of third-party open source components
Log framework, Nlog and Log4 net.
2. Serialize the component json.net.
How to create Windows services using open source Topshelf components.
V. Basic use of SmartThreadPool
5.1 Github source code address:
https://github.com/amibar/SmartThreadPool
5.2 NuGet for online downloads:
Install-Package SmartThreadPool.dll
5.3 Demonstrate how SmartThreadPool implements waiting for multiple Job tasks to complete
After reading the above, do you have any further understanding of the example analysis of multitasking batch processing implemented by. NET thread pool technology? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.