Is it highly efficient to use multithreading? Sometimes multithreading is not used to improve efficiency, but rather to enable the CPU to handle multiple events at the same time.
Use Scenario 1
Why not block the main thread and start other threads to do time-consuming things.
For example, the time-consuming operation in app development is not done in the main thread of the UI.
Use Scenario 2
An application that implements a faster response, that is, the main thread listens specifically to user requests, which are used by child threads to process user requests. To get a large throughput.
In this case, the efficiency of multithreading is not necessarily high. In this case, multithreading is done so that multiple data can be processed in parallel without waiting.
For example, Javaweb is the main thread that listens specifically to the user's HTTP request, and then initiates a child thread to process the user's HTTP request.
Use Scenario 3
A service that has a low priority, but does not do it regularly.
such as garbage collection of the JVM.
Use Scenario 4
Some tasks, although time-consuming, but not CPU-intensive operations, open multiple threads, the efficiency will be significantly improved.
such as reading files, and then processing. Disk IO is a time-consuming task that does not consume CPU computing. So one thread can read the data, and one thread processes the data. Definitely better than
One thread reads the data, and then the processing efficiency is high. Because two threads take advantage of the CPU's idle time waiting for disk IO.
I thought about using multithreading to read disk data, but the performance bottleneck of reading disk data is IO, not CPU. The purpose of multithreading is to not allow the CPU to be idle, and it is obviously not suitable for reading disk data.
Java Multithreaded Usage Scenarios