JVM program counter (PC register)

JVM program counter (PC register)

Insert picture description here

Introduction to pc Register

In the Program counter Register in the JVM, the name of Register comes from the register of the CPU, and the register stores field information related to instructions. The CPU can only run by loading data into registers.
Here, it is not a physical register in a broad sense. Perhaps it will be more appropriate to translate it into a PC counter (or instruction counter) (also called a program hook), and it is not easy to cause unnecessary misunderstandings. The Pc register in the JVM is an abstract simulation of the physical PC register.

effect:

The PC register is used to store the address of the next instruction, which is the instruction code to be executed. The execution engine reads the next instruction.

Insert picture description here

  • It is a small memory space, almost negligible. It is also the fastest storage area.
  • In the JVM specification, each thread has its own program counter, which is private to the thread, and its life cycle is consistent with the life cycle of the thread.
  • There is only one method in a thread at any time, which is the so-called current method. The program counter stores the JVM instruction address of the Java method being executed by the current thread; or, if the native method is executing, it is an undefned value (undefned).
  • It is an indicator of program control flow. Basic functions such as branches, loops, jumps, exception handling, and thread recovery all need to rely on this counter to complete.
  • When the bytecode interpreter works, it selects the next bytecode instruction to be executed by changing the value of this counter.
  • It is the only area that does not specify any outOtMemoryError conditions in the Java Virtual Machine Specification. (No GC, OOM)

Insert picture description here

Two common interview questions:

Question one

  • What is the use of using the PC register to store the bytecode instruction address?
    -Because the CPU needs to constantly switch between threads, after switching back at this time, it has to know where to continue execution.
  • Why use the PC register to record the execution address of the current thread?
    • The JVM bytecode interpreter needs to change the value of the Pc register to clarify what bytecode instruction should be executed next.

Insert picture description here

Question two:

Why is the PC register set as thread private?

  • We all know that the so-called multi-threading method will only execute one of the threads in a specific period of time. The CPU will constantly switch tasks, which will inevitably lead to frequent interruptions or recovery. How to ensure that the points are correct? In order to be able to To accurately record the current bytecode instruction address being executed by each thread, the best way is naturally to allocate a Pc register for each thread, so that each thread can perform independent calculations, so that there will be no mutual interference. Disturbing situation.
  • Due to the limitation of CPU time slices, in the concurrent execution of many threads, at any certain moment, a processor or a core in a multi-core processor can only execute one instruction in a certain thread.
  • This will inevitably lead to frequent interruption or recovery. How to ensure that there is no difference between the points? After each thread is created, it will generate its own program counter and stack frame, and the program counter does not affect each other among the threads.

Insert picture description here

##CPU time slice:

  • The CPU time slice is the time allocated by the CPU to each program. Each thread is assigned a time period, which is called its time slice.
  • At the macro level: we can open multiple applications at the same time, and each program runs in parallel without contradiction.
  • But at the micro level: Since there is only one CPU, it can only process part of the program requirements at a time. One way to deal with fairness is to introduce time slices, and each program executes in turn.

Parallel: Similar to serial, queuing, there is a priority!

Concurrency: Similar to parallel, happening at the same time!

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108671744