Program Counter (PC Register)

The Java Virtual Machine can support many threads of execution at once (JLS §17). Each Java Virtual Machine thread has its own pc (program counter) register. At any point, each Java Virtual Machine thread is executing the code of a single method, namely the current method (§2.6) for that thread. If that method is not native, the pc register contains the address of the Java Virtual Machine instruction currently being executed. If the method currently being executed by the thread is native, the value of the Java Virtual Machine’s pc register is undefined. The Java Virtual Machine’s pc register is wide enough to hold a returnAddress or a native pointer on the specific platform.

The Programe Counter Register in the JVM is similar to the Register in the CPU, which stores the field information related to the instructions. The CPU can only execute the instructions properly if the data is stored in the Register.

Registers in the JVM are not physical registers but abstract simulations of physical registers.

Program counters are also called program hooks

role

The PC register is used to store the address pointing to the next instruction,

Only one method is executing per thread at any time, and the counter stores the JVM instruction address of the method being executed by the current thread

The characteristics of

  • Small memory area, fast reading (similar to CPU registers)
  • Unique to each thread, the same as the lifetime of the thread
  • Is responsible for recording the current execution position of each thread
  • If the local method is executed, the counter stores undefined
  • The only area Of the JVM that does not have an Out Of Memory Error does not use garbage collection

Frequently seen exam

Q: Why use a PC register to record the address of an instruction?

A: Because the CPU keeps switching threads during program execution, the thread must know which instruction to execute next after switching back.

Q: Why are PC registers thread private

A: The CPU is constantly switching between threads. If PC registers are shared, such as thread 1 switching to thread 2, thread 2 will overwrite the address of the instruction that thread 1 will execute