编程知识 cdmana.com

Java JVM -- 4. Program counter

brief introduction

  JVM Program count register in (Program Counter Register),Register The name of comes from CPU The register of , Registers store field information about instructions ,CPU Only loading data into registers can run .

   Here's the register , It's not a physical register in a broad sense , Translate it into PC Counter ( Or instruction counter ) More appropriate ( Also known as program hook ), And it's not easy to cause some unnecessary misunderstandings .JVM Medium PC Registers are for physics PC An abstract simulation of registers .

PC register

   Program counter is a very small memory space , I can almost ignore it , It's the fastest running storage area .

   stay JVM Specification , Each thread has its own program counter , Threads are private , The life cycle is consistent with the life cycle of the thread .

   There is only one method in a thread at any time , It's the so-called current approach . The program counter stores what the current thread is executing Java Methodical JVM Bytecode instruction address ; If the method performs Native Method , The value of the program counter is null (Undefined).

   It is an indicator of the flow of program control , Branch 、 loop 、 Jump 、 exception handling 、 Basic functions such as thread recovery rely on this counter . The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to execute .

   It is the only one in Java There is no  Out Of Memory( out of memory ) Area of situation .


effect

  PC Registers are used to store the address to the next instruction , Also about to execute the instruction code . The execution engine reads the next instruction .

Case presentation :

public class PCRegisterTest {
    public static void main(String[] args) {
        int i = 10;
        int j = 20;
        int k = i + j;
    }
}

Compiled bytecode :

   Compile the code into a bytecode file , We did this by looking at , Find a line number to the left of bytecode , It's the instruction address , Used to point to where the current execution is .

Working principle diagram


Use PC What's the use of registers to store bytecode instruction addresses ?

CUP Thread scheduling

   because CPU You need to switch threads all the time , After switching from one method to another , You need to know where to start next .JVM The bytecode interpreter can read PC Register to determine what kind of bytecode instruction should be executed next .


PC Why is the register set to private ?

Thread PC register

   We all know that the so-called multithreading will only execute one of the methods in a specific period of time ,CPU I'll keep switching tasks , This inevitably leads to frequent interruptions or recoveries , How to make sure there is no difference ?

   In order to accurately record the current bytecode instruction address that each thread is executing , The best way, of course, is to assign one to each thread PC register , In this way, each thread can be independently calculated , So that there will be no mutual interference .


CPU Time slice

  CPU Time slice , namely CPU Time allocated to each program , Each thread is assigned a time period , It's called a time slice .

   On the macro : We can open multiple applications at once , Each program runs parallel , Running at the same time .

   On the micro level : Since there is only one CPU, Only part of the program's requirements can be processed at a time , How to deal with fairness , One way is to introduce time slices , Each program is executed in turn .

 

 

 

版权声明
本文为[There's a bastard in the north of the city]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224091951899x.html

Scroll to Top