[JVM] Fourth, the program counter (PC register)

Hello, everyone, I am a pig who is dominated by cabbage.

A person who loves to study, sleepless and forgets to eat, is obsessed with the girl's chic, calm and indifferent coding handsome boy.

If you like my text, please follow the public account "Let go of this cabbage and let me come"

04-Program counter (PC register)

1. Introduction to PC Register

In the Program Counter Register in the JVM, the name of Register is derived from
the register of the CPU, and the register stores field information related to instructions. The CPU can only run by loading data into the registers.

Here, it is not a physical register in a broad sense. Perhaps it is 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.

How to understand the hook? Like Beijing roast duck, every duck has a hook. Just hook up which duck you want to eat. The hooks here can also be understood in the same way and are used to refer to which programs should be executed.

The PC register is used to store the address pointing to the next instruction, that is, the instruction code to be executed will be read by the execution engine to
fetch the next instruction.

  • It is a small memory space, almost negligible. It is also the
    storage area with the fastest running speed .
  • 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 will store the JVM instruction address of the Java method being executed by the current thread; or, if the native method is being executed, it is an undefned value (undefned). (PC Register is at the Java level, and cannot be displayed when calling the C language level)
  • 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 that needs 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 .

There is neither GC nor OOM, GC in the heap and method area, and none in the others.

2. Examples

Insert picture description here

Three, two common problems

What is the use of using the PC register to store the bytecode instruction address?
Why use the PC register to record the execution address of the current thread?

(What are the benefits of having a girlfriend? Why should there be a girlfriend?)

Because the CPU needs to constantly switch between threads, after switching back at this time, it has to know where to continue execution.
The JVM bytecode interpreter needs to change the value of the PC register to determine what bytecode instruction should be executed next.

Insert picture description here

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, and the CPU will constantly switch tasks, which will inevitably lead to frequent interrupts or replies. How to ensure that the score is correct? In order to be able to prepare to record the current bytecode instruction address being executed by each thread, the best method is naturally to allocate a PC register for each thread, so that independent calculations can be performed between each thread, so that there will be no The situation of mutual interference.

Due to the limitation of the CPU time slice, in the concurrent execution of many threads, at any certain moment, a processor or a core in a multi-core processor will only execute one instruction in a certain scene.

This will inevitably lead to frequent interruptions and gray levels. How to ensure that the points are correct? 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.

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 is executed in turn.

Parallel: vs serial

Concurrency: vs

Insert picture description here

Concurrency means that two queues alternately use one coffee machine, and parallel means that two queues use two coffee machines at the same time. If serial, one queue uses one coffee machine, then even if the person in front is constipated and goes to the toilet for a long time, the latter People can only wait for him to come back to pick up coffee. This efficiency is undoubtedly the lowest.

Is concurrency a thread, parallel is multiple threads?
Concurrency and parallelism can be many threads, depending on whether these threads can be executed by (multiple) cpu at the same time, if possible, it means parallelism, and concurrency is that multiple threads are executed (One) The cpu alternates execution.

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/111947652