25.8 reasons why reasons to use threads
The thread is used for three reasons:
●You can use the threadCodeIsolated from other codes. This will improve your applicationProgramReliability. In fact, this is why WindowsMediumThe reason why thread concepts are introduced. Windows requires threads to obtain reliability because your application is a third-party component for the operating system, and Microsoft will not verify the quality of the code before you release the application. But before release, you should test all your applications on your own. In addition, since you have tested the complete applications, you should know that they are robust and high-quality. Because your applications do not require high robustness as the operating system does not, your applications should not use too many threads for the purpose of maintaining robustness. If your application supports loading components generated by other vendors, the robustness requirement of the application will be improved.
●You can use threads to simplify encoding.. Sometimes, if a task is executed in its own thread, the encoding becomes simpler. However, in this case, additional resources must be used, and the code is not very"Economic". Now, even if I have to pay some resources as a price, I would rather choose a simpler encoding process. Otherwise, I simply insisted on writing programs in machine language, there is no need to become a C # developer. But sometimes, when using threads, some people feel that they have chosen a more easy way of programming, but in fact they have greatly complicated things (and their code. Generally, when you introduce a thread, you introduce code that requires mutual cooperation. They may require the thread to synchronously construct the code to know when the other thread is terminated. Once collaboration is involved, more resources are required and the code becomes more complex. Therefore, before starting to use the thread, make sure that the thread can help you.
●Concurrent execution can be implemented using threads.. Only when you know that your applications are running on multiple CPU machines can you enable multiple tasks to run at the same time to improve performance. Currently, machines with multiple CPUs (or one multi-core CPU) installed are quite common, so it makes sense to design applications to use multiple kernels, this will be the subject of chapter 26th and Chapter 27th.
Now I want to share my views with you. Each computer actually has a very powerful resource:CPU. If a person spends money on a computer, the computer should24Working hours. In other words, I think allCPUThe usage should always be in100%. However, there are two exceptions: The first case is that when your computer has insufficient power, and the second case is that the data center prefers10Platform50%Do not use computers5Platform100%The computer in which the usage is made has too much heat dissipation.
If you agree with me, the next step is to letCPUWhat is appropriate. Before I tell you what I think, let me start with something else. In the past, developers and users always felt that computers were not powerful enough. Therefore, our developers will not let the code be executed unless the user passesUIElement to grant permissions to developers, allowing applications to start consumingCPUResources.
Now it is different. Computers have very powerful computing capabilities and will become more powerful in the near future. In the previous sections of this chapter, I have shown you how to view myCPUUsage. If my computer uses quad-coreCPUInstead of dual-core, the "Task Manager" will report more time0%. When80When the core processor comes out, our computer will look idle. For computer buyers, it is like spending more money to buy more powerfulCPUBut computers are getting fewer and fewer jobs!
This is also the reason why hardware vendors are difficult to sell: the software is not fully utilizing the hardware, and the user does notCPUBenefits. In my opinion, the current computing power is very surplus, and the surplus is growing, so developers should boldly consume it. It's time to change our thinking-in the past, unless the end user is sure to get a computing result, we will not be able to execute this computing. But now with additional computing capabilities, developers can be more bold.
See the following example:VSWhen you stop editing in the editor,VSThe compiler is automatically run and your code is compiled. This improves the production efficiency of developers because they can be seen during input.Source codeAnd can immediately correct the problem. In fact, the traditional "edit-generate-Debug" mode will gradually change.To "edit"-Debugging"Mode, because the generation (Compilation) code is always happening. As a user, you cannot pay attention to this because there are a lot of CPU capabilities available, and the frequent running of the compiler will not affect other things you are doing. In fact, I want to completely remove the "generate" menu in future vs versions, as this process becomes completely automated. This not only makes the UI of the application simpler, but also enables the application to automatically provide the "Answer" to users, making their development more efficient.
If we remove a UI element like a menu item, the computer will become easier for users. They face fewer options and fewer concepts that need to be identified and understood. The multi-core Revolution allowed us to remove these UI elements, making the software simpler and making my grandmother comfortable using computers one day. After a developer removes a UI element, the required number of tests is generally reduced. In addition, users are provided with fewer options to simplify encoding. If you need to localize the UI elements and documents (just like Microsoft), removing the UI elements means that fewer documents need to be written, and you do not need to localize the reduced documents. All this can save a lot of time and money for your company.
Below are some examples of active CPU consumption capabilities: document spelling check and syntax check, spreadsheet re-calculation, disk file index to improve search speed and hard disk fragmentation to improve I/O performance.
I want to live in a simple UI world. I want to have more screen space to display the data I actually processed. The application should take the initiative to provide more information to help me quickly and efficiently complete the work, instead of telling the application to obtain information each time. Over the past few years, hardware has quietly waited for software developers to explore their potential. Now, it's time to use hardware creatively!