"High concurrency and multi-threading" are always mentioned together, giving the impression that the two are equal, but in fact high concurrency ≠ multi-threading
Multi-threading(Recommended learning: web front-end video tutorial)
Multi-threading is a feature of java, because now the CPU It is multi-core and multi-threaded and can perform several tasks at the same time. In order to improve the execution efficiency of JVM, Java provides this multi-threading mechanism to enhance data processing efficiency. Multi-threading corresponds to the CPU, and high concurrency corresponds to access requests. You can use a single thread to process all access requests, or use multiple threads to process access requests at the same time.
In the past single-CPU era, a single task could only execute a single program at a point in time. Later, it developed into the multi-tasking stage, where the computer can execute multiple tasks or processes in parallel at the same point in time.
Although it is not the "same point in time" in the true sense, multiple tasks or processes share a CPU, and it is left to the operating system to complete the running switching of the CPU between multiple tasks, so that each Tasks have a chance to run within a certain time slice.
Later, multi-threading technology developed, allowing multiple threads to execute in parallel within a program. The execution of a thread can be thought of as a CPU executing the program. When a program runs under multi-threading, it appears as if multiple CPUs are executing the program at the same time.
In short, multi-threading can be understood this way: multi-threading is a programming method for dealing with high concurrency, that is, concurrency needs to be implemented with multi-threading.
High concurrency
High concurrency is not something exclusive to JAVA. It is a broad, language-independent concept proposed to provide better Internet services. .
Typical scenarios, such as: 12306 grabbing train tickets, Tmall Double Eleven flash sale event, etc. The occurrence of this situation will cause the system to perform a large number of operations during this period, such as requests for resources, database operations, etc. If high concurrency is not handled well, it will not only reduce the user experience (the request response time is too long), but may also cause system downtime. In severe cases, it may even cause OOM exceptions and the system to stop working.
If you want the system to be able to adapt to high concurrency, you need to optimize the system from all aspects, including hardware, network, system architecture, selection of development language, use of data structures, algorithm optimization, Database optimization, etc... and multi-threading is just one of the solutions.
The above is the detailed content of Multi-thread concurrency. For more information, please follow other related articles on the PHP Chinese website!