First, the concept of the operating system thread and process
- The operating system now is a multitasking operating system. Multithreading is a way to achieve multitasking.
- A process is an application running in memory in which each process has its own separate piece of memory, and a process can start multiple threads. For example, in a Windows system, a running EXE is a process.
- A thread is an execution process in a process that can run multiple threads in a process. For example, a java.exe process can run many threads. Threads are always part of a process, and multiple threads in a process share the memory of the process. "Simultaneous" execution is a thread-to-human sensation that is actually rotated between threads.
Second, threads in Java
- There are two ways to create multithreading: Inherit the thread class and implement the Runnable interface.
- A thread class instance is just an object that, like any other object in Java, has variables and methods that live and die on the heap.
- In Java, each thread has a call stack, and the thread runs in the background even if no new threads are created in the program.
- A Java application always runs from the main () method, and the Mian () method runs inside a thread, which is called the main thread.
- Once a new thread is created, a new call stack is generated.
- Thread population is divided into two categories: user thread and waiting thread
- The JVM shuts down automatically when all user threads have finished executing. But the waiting thread is not independent of the JVM, and the waiting thread is typically created by the operating system or the user itself.
Java multithreading-Concepts and principles