Processes and threads

Process and thread concepts:

** process: ** is the smallest unit of execution of the operating system, with a process created in memory for every application started. A process includes: ** One or more threads, stacks, and method areas. ** process crashes do not affect each other.

Thread:Is the smallest unit of the CPU to perform a task. A thread is a structure of a process in which multiple threads share the same processHeap, method areaThe data. A thread crash kills the entire process.

Structure of processes and threads

The structure of the process includes:Heap, method area, threadHeap is storedCreate an objectThe place where the method area is storedConstants, static variables, loaded class information, etcPlace. The heap and method areas are shared data between multiple threads of the same process.

It can be seen from the above figure that the structure of threads includes:Program counter, virtual machine stack, local method stack. Program counter: Records the thread execution position, after switching threads can be started at the corresponding position, rather than restarting execution. Vm stack: RunJava layer method, the method will start and finish, there will be corresponding on and off the stack. Local method stack: executesNative layer method

Number of CPU cores and number of threads

One CPU core corresponds to one physical thread, and one physical thread can simulate two threads through hyperthreading technology, so a 2-core CPU can simulate four threads, and a 4-core CPU can simulate eight threads.

CPU time slice rotation

As you can see from the processes and Threads graph above, there is only one CPU and only one process can be executed at a time. So processes can queue up to execute the CPU when there are multiple processes. The amount of time allocated by the CPU to a process is called the time slice. If the process has not completed its execution by the end of the time slice, the CPU records the execution status of this process to prepare for the next execution and allocates the CPU to the next process in the queue. If the process ends or blocks before the time slice ends, the CPU immediately switches to another process. CPU process switching is also called context switching, and each process has a very short time slice, and the information to be saved in context switching consumes a lot of time. Therefore, the time slice should not be too short or too long. If the time slice is too short, it will cost a lot of saving work if The Times of switching increase. If the time slice is too long, the process will wait too long for execution, which will affect the user experience.

The concept of concurrency and parallelism

Concurrency is A program structure that indicates that threads can execute alternately, for example, halfway through thread A, then set aside to start thread B, and then execute thread A again.

Parallelism means that multiple threads can execute simultaneously at the same time.

thread

Java threads can be implemented in two ways, one is to inherit Thread, the other is to implement Runnable. Both methods perform time-consuming operations in the run() method. By calling the start() method on a thread object, the thread is in the process waiting for the CPU call to execute. This state is also called the ready state.

Threads can be divided into ** : new state, ready state, running state, blocked state, and finished state. The following figure shows the situation of the five states of threads.

**new Ready state of a thread object instance: by calling the thread object.start() method, the thread enters the ready state, waiting to be executed in the process. ** Running status: ** The thread has received CPU execution resources. ** Blocked: The thread is blocked when it calls the sleep() method, the IO request, the wait() method, or the synchroized lock. ** Dead: A thread enters the dead state when its execution ends or is abnormal.

** Sleep (Long millis):** causes the thread sleep to block and enters the ready state to recompete for the CPU after sleep is over.

Wait (), wait(long millis): Blocks the current thread until another thread calls notify() of this object or reaches the ready state after the set wait time.

** JOIN ():** When the current thread calls the join() method of another thread, the current thread blocks and is not ready until the other thread completes

**yield():** Suspends the executing thread object and transitions to a ready state, allowing other queued threads to execute CPU resources.

** Interrupt ():** sets the thread’s interrupt flag bit to true, but it does not interrupt the executing thread. Instead, it interrupts after isInterrupted(). If the thread is blocking after calling wait/sleep/ JOIN, InterruptedException events are emitted.

**setDaemon:** Sets the thread to the background (daemon) thread, which is the thread that serves other threads, and the foreground thread is the thread that receives the service.

SetPriority: Sets the priority of a thread. The value ranges from 1 to 10. A normal thread has a priority of 5