1: To understand multi-threading, you must first understand the thread, and to understand the thread, you must first understand the process, because the thread is dependent on the process and exists.
2:
through Task Manager we see the existence of the process.
and by observing it, you can see that only the running program will appear in the process.
Process: Is the program that is running. A
process is an independent unit of system resource allocation and invocation. Each process has its own memory space and system resources.
3:
A single-process computer can do only one thing, and we now have computers that do multiple things.
Example: While playing the game (the game process), while listening to music (music process).
This means that computers now support multiple processes and can perform multiple tasks within a single time period.
and, you can increase the CPU utilization.
Question: Is
playing the game while listening to music at the same time?
No. Because a single CPU can only do one thing at a certain point in time.
While we are playing a game, or listening to music, the CPU is doing a program between the efficient switching let us feel that it is done at the same time.
4: The
can perform multiple tasks within the same process, and each of these tasks I can see is a thread.
Thread: Is the execution unit of the program, the execution path. is the most basic unit of the CPU used by the program.
Single Thread: If the program has only one execution path.
Multithreading: If the program has multiple execution paths.
5: What's the point of multithreading? The
does not increase the execution speed of the program. In fact, to improve application usage. The execution of the
program is actually in the grab CPU resources, CPU execution right.
Multiple processes are robbing this resource, and one of those processes has a higher chance of grabbing CPU execution if the execution path is much larger.
We are not sure which thread will be able to grab at any time, so the execution of the thread is random.
Process:
A running program is an independent unit of system resource allocation and invocation.
Each process has its own memory space and system resources.
Thread:
is a single sequential control flow in the process, which is an execution path
A process, if there is only one execution path, is called a single-threaded procedure.
A process that has multiple execution paths is called a multithreaded program.
Example:
Minesweeper program, Thunder download
Notice the difference between the two words: parallel and concurrent .
The former is logically simultaneous, which means running multiple programs at the same time within a certain period.
The latter is physically occurring at the same time, which refers to running multiple programs simultaneously at a certain point.
How the Java program works:
The Java command launches the Java Virtual machine, starting the JVM, which is equivalent to launching an application, that is, initiating a process.
The process automatically starts a "main thread" and then the main thread calls the main method of a class.
So the main method runs in the main thread. All programs prior to this are single-threaded.
Study Questions
Is the startup of the JVM virtual machine single-threaded or multithreaded?
Multi-threaded.
The reason is that garbage collection threads also have to start first, otherwise memory overflow can occur easily.
Now the garbage collection thread plus the previous main thread, the minimum boot two threads, so the JVM startup is actually multi-threaded.
Java 22-3 Multi-Threading Overview and other things involved (see)