Basic thread understanding (1) (java), basic thread understanding java
I. Basic concepts of Multithreading
A thread is an execution scenario in a process, that is, an execution process. Therefore, we need to first talk about the process and the relationship between the Process and the thread.
1. What is a process?
A process corresponds to an application. If we start Word in windows, a process is started. Starting JVM in java development environment means a process is started. Modern computers support multiple processes. In the same operating system, multiple processes can be started at the same time. So what is multi-process?
2. What is multi-process? What is the role of multi-process?
A single-process computer can only do one thing. Just like a computer that was a long time ago, only one Dos window can be opened. This is a single-process computer.
While you listen to music while playing lol (lol process), this is multi-process. So the question is, are game and music processes running at the same time on a single-core computer?
No. (Multi-core computers can run at the same time, so the processing speed is high.) because the CPU of a computer can only do one thing at a certain time point, because the computer can frequently switch between the "game process" and "Music process", the switching speed is extremely high, and humans feel that the game and music are both in progress. There are many examples of this in life. In the past, through rapid page flip, people moved. When a movie was played one frame at a time, the screen moved, and humans felt like they were doing it at the same time, in fact, it is through the fast switching of CPU, people feel like this. Therefore, the function of multi-process is not to increase the execution speed, but to improve the CPU usage. In the past, one CPU can only do one thing. Now, you can switch between different processes to achieve multiple tasks.
3. What is thread and connection with process?
A thread is an execution scenario in a process. A process can start multiple threads. It can be understood that a process can contain multiple threads.
4. What is the role of multithreading?
Similarly, multithreading and multi-process functions the same, not to speed up execution, but to improve application usage, just as three people access the Baidu server at the same time (enter the Baidu website), the server-side program is
Process. If it is a single thread, you will not be able to access Baidu server when I access it. If each of us occupies a thread, the server will frequently switch between us, it gives us the illusion that multiple threads are concurrently executed.
Many people can access the Baidu website at the same time. Threads and threads share "heap memory and method zone memory". The stack memory is independent. One thread and one stack.
5. How does java program work?
The java command starts the java Virtual Machine and starts the JVM. It means that an application is started and a process is started. This process automatically starts a "main thread" and then the main thread calls the main method of a class.
The main method runs in the main thread.
2. How to Implement multithreading in java
1. The first method to implement multithreading in java:
(1) inherit java. lang. Thread
(2) rewrite the run method.
Three knowledge points:
How to define a thread? Inherit Thread class
How to Create a thread? New subclass call
How to start a thread? . Start () method
2. The second method to implement multithreading in java:
(1) Write a class to implement the java. lang. Runnable interface
(2) run Method
3. Thread Lifecycle