In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the advantages of java multithreading". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Despite its many challenges, multithreading has several advantages that keep it in use. These advantages are:
Better resource utilization
Programming is simpler in some cases
The program responds faster
Better resource utilization
Imagine an application that needs to read and process files from a 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 file A
2 seconds to process file A
5 seconds to read file B
2 seconds to process file B
---------------------
14 seconds total.
When reading files from disk, most CPU time is spent waiting for the disk to read the data. During this time, the CPU is very idle. It can do something else. By changing the order of operations, CPU resources can be better used. Look at the following order:
5 seconds to read file A
5 seconds to read file B + 2 seconds to process file A
2 seconds to process file B
---------------------
12 seconds total.
The CPU waits for the first file to be read. Then start reading the second file. While the second file is being read, the CPU processes the first file. Remember that the CPU is idle most of the time while waiting for disk to read files.
In general, the CPU can do something else while waiting for IO. This doesn't have to be disk IO. It can also be network IO, or user input. In general, network and disk IO is much slower than CPU and memory IO.
Simpler programming
In a single-threaded application, if you want to write a program that manually handles the order of reads and processes mentioned above, you must record the state of each file read and process. Instead, you can start two threads, each handling reads and operations on a file. Threads get blocked waiting for disk to read files. While waiting, other threads can use the CPU to process files that have already been read. As a result, the disk is busy reading different files into memory. This results in disk and CPU utilization improvements. And each thread only needs to record one file, so this method is also easy to program.
The program responds faster
Another common goal of turning a single-threaded application into a multithreaded application is to achieve a more responsive application. Imagine a server application that listens for incoming requests on a certain port. When a request arrives, it processes the request and then goes back to listen.
The flow 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 only be received if the server is listening. Another design is for the listening thread to pass the request to the worker thread and then immediately return to listening. The worker thread can process the request and send a reply to the client. This design is described below:
while(server is active){
listen for request
hand request to worker thread
}
This way, the server-side thread quickly returns to listening. As a result, more clients are able to send requests to the server. The service has also become more responsive.
The same goes for desktop apps. If you click a button to start a time-consuming task, the thread will perform the task while updating windows and buttons, and the application will appear to be unresponsive as the task executes. Instead, tasks can be passed to worker word threads. While worker threads are busy with tasks, window threads are free to respond to requests from other users. When the worker thread completes the task, it sends a signal to the window thread. The window thread updates the application window and displays the results of the task. To users, programs designed with worker threads appear to be more responsive.
What are the advantages of Java multithreading? The content is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.