In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "what are the advantages of server multithreading". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what are the advantages of server multithreading" can help you solve the problem.
Despite many challenges, multithreading has some advantages that keep it in use. These advantages are:
Better utilization of resources
Programming is simpler in some cases
The program responds faster
Better utilization of resources
Imagine an application that needs to read and process files from the local file system. For example, it takes 5 seconds to read a file from disk and 2 seconds to process a file. Processing two files requires:
5 seconds to read files A2 seconds to process files A5 seconds to read files B2 seconds to process files B-A total of 14 seconds
When reading files from disk, most of the CPU time is spent waiting for the disk to read data. During this time, CPU is very free. It can do something else. By changing the order of operations, you can make better use of CPU resources. Look at the following order:
5 seconds to read files A5 seconds to read files B + 2 seconds to process files A2 seconds to process files B-A total of 12 seconds
CPU waits for * files to be read. Then start reading the second file. When the second file is read, CPU processes * * files. Remember that CPU is idle most of the time while waiting for the disk to read the file.
In general, CPU can do something else while waiting for IO. This is not necessarily the disk IO. It can also be the IO of the network, or user input. In general, the IO of networks and disks is much slower than that of CPU and IO in memory.
Programming is simpler.
In a single-threaded application, if you want to write a program to manually handle the order of reading and processing mentioned above, you must record the status of each file read and processed. Instead, you can start two threads, each of which handles reading and manipulating a file. The thread is blocked while waiting for the disk to read the file. While waiting, other threads can use CPU to process files that have already been read. As a result, the disk is always busy reading different files into memory. This leads to an increase in disk and CPU utilization. And only one file needs to be recorded per thread, so this approach is also easy to program.
The program responds faster
Another common purpose of turning a single-threaded application into a multithreaded application is to implement a more responsive application. Imagine a server application that listens for incoming requests on a port. When a request comes, it processes the request and then goes back to listen.
The process of the server is as follows:
While (server is active) {listen for request process request}
If a request takes a lot of time to process, the new client cannot send the request to the server during that time. Requests can be received only when the server is listening. In another design, the listener thread passes the request to the worker thread (worker thread) and then returns immediately to listen. The worker thread can process the request and send a reply to the client. This design is as follows:
While (server is active) {listen for request hand request to worker thread}
In this way, the server thread quickly returns to listen. As a result, more clients can send requests to the server. The service has also become more responsive.
The same is true of desktop applications. If you click a button to start running a time-consuming task and the thread not only executes the task but also updates the windows and buttons, the application program seems to be unresponsive during the execution of the task. Instead, the task can be passed to the worker thread (word thread). When the worker thread is busy with the task, the window thread is free to respond to requests from other users. When the worker thread finishes the task, it sends a signal to the window thread. The window thread can update the application window and display the results of the task. For users, this kind of program with worker thread design appears to be more responsive.
Moving from a single-threaded application to a multithreaded application does not only bring benefits, it also has some costs. Don't use multithreading just for the sake of multithreading. Instead, it should be clear that multithreading is used only when the benefits of using multithreading outweigh the cost. If in doubt, you should try to measure the application's performance and responsiveness, rather than just guessing.
The design is more complex
While some multithreaded applications are simpler than single-threaded applications, others are generally more complex. This part of the code requires special attention when multithreading accesses shared data. Interactions between threads are often very complex. Errors caused by incorrect thread synchronization are very difficult to detect and reproduce to fix.
Cost of context switching
When CPU switches from one thread to another, it needs to store the local data of the current thread, program pointers, etc., and then load the local data of another thread, program pointers, etc., before execution begins. This switch is called "context switching" ("context switch"). CPU executes one thread in one context and then switches to another context to execute another thread.
Context switching is not cheap. If it is not necessary, the occurrence of context switching should be reduced.
This is the end of the content about "what are the advantages of server multithreading". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.