CPU, processes, threads, stack, heap, the method area relationship (JAVA)

1. What threads are? What processes are?

A thread is a physical process, the thread itself is not an independent existence.
Process code that is run on the active data set, the system resource allocation and scheduling the amount of the basic unit, the thread is a path of execution process. At least one process multiple threads share the process resources a thread in the process.

The operating system at the time of allocation of resources is the resources allocated to the process, but the CPU resources rather special, it is assigned to the thread. Because really want to take up CPU running a thread, also said thread is the basic unit of the CPU.

2. The relationship between process and thread

In Java, when we started, when in fact the main function is to start the process of a JVM, while the main function where the thread is a thread in that process, also known as the main thread.


13226573-23dd01877d88cfba.png
The relationship between process and thread

A process has multiple threads, multiple threads share the resources in the process heap and method area. But each thread has its own program counter and stack area.

The program counter is a memory area for the current instruction address logging thread to be executed, in the final thread private.
CPU generally use round-robin manner so that the thread polling occupation, so after the current thread CPU time slice runs out will make the CPU. Wait until the next polling time and then executed.
The program counter is recorded when the thread let out when the CPU performs address, redistribution to wait until the next time when the film thread can continue execution from the specified address on its own private counter.

3. Java's native method

Simply put, a Native Method is a non-call interface java java code. A Native Method is a method java: implementing the method is implemented by a non java language, such as C. This feature is not unique to java, many other programming languages ​​have this mechanism, such as in C ++, you can use extern "C" inform the C ++ compiler to call a C function of.

After the introduction is over Native method, followed by the implementation of the above program counter if the Native way, then counter pc record is undefined address, only the implementation of a counter pc is recorded when the Java code for
the next instruction address.

In addition, each thread has its own stack resources for storing local variables that thread, these local variables is the line
Cheng private, other threads can not be accessed, in addition to branches is also used to store the call stack thread frame.
A process heap is the largest piece of memory, heap all the threads are in the process of sharing, is the process to create a division
with a heap inside the main storage object instance is created using the new operator.
The method area is used to store information category, constants and variables such as JVM static load, and it is shared by the threads.

Guess you like

Origin blog.csdn.net/weixin_33873846/article/details/91013955