Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and function of java Program counter

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the introduction and function of java program counter". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the introduction and function of java program counter".

Introduction to program counter

In the program count register (Program Counter Register) in JVM, the name of Register is derived from the register of CPU, which stores the field information related to instructions. CPU can only run if the data is loaded into a register. Here, it is not a physical register in a broad sense, but it may be more appropriate to translate it into a PC counter (or instruction counter) (also known as a program hook), and it is not easy to cause unnecessary misunderstandings. The PC register in JVM is an abstract simulation of the physical PC register.

Action

The PC register is used to store the address that points to the next instruction and the instruction code to be executed. The next instruction is read by the execution engine.

It is a small piece of memory space that can almost be ignored. 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 the life cycle is consistent with the life cycle of the thread.

There is only one method executing a thread at any time, that is, the so-called current method. The program counter stores the JVM instruction address of the Java method that the current thread is executing, or, if the native method is executing, an unspecified value (undefined).

It is the indicator of program control flow, branch, loop, jump, exception handling, thread recovery and other basic functions need to rely on this counter to complete.

The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to be executed.

It is the only area where no OutotMemoryError situation is specified in the Java virtual machine specification.

Code demonstration

Let's start with a simple code.

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

Then the code is compiled into a bytecode file, and we look at it again and find that there is a line number identification on the left side of the bytecode, which is actually the instruction address, which is used to point to where it is currently executed.

0: bipush 102: istore_13: bipush 205: istore_26: iload_17: iload_28: iadd9: istore_310: return

Through the PC register, we can know where the current program has been executed.

Two FAQ 1 what is the use of using PC registers to store bytecode instruction addresses? Why use the PC register to record the execution address of the current thread?

Because CPU needs to keep switching threads, when you switch back, you need to know where to start next.

JVM's bytecode interpreter needs to change the value of the PC register to determine what bytecode instruction should be executed next.

2 Why is the PC register set to private?

We all know that the so-called multithreading will only execute one of the threads in a specific period of time, and CPU will keep switching tasks, which will inevitably lead to frequent interruptions or recoveries. How can we make sure that the score is the same? In order to accurately record the current bytecode instruction address that each thread is executing, the best way is to assign a PC register to each thread, so that each thread can calculate independently without interference with each other.

Due to the time wheel limitation of CPU, many threads will only execute one instruction in a thread at any given time, a processor or a core in a multi-core processor.

This will inevitably lead to frequent interruptions or recovery. how can we ensure that there is no difference? After each thread is created, it will generate its own program counter and stack frame, and the program counter does not affect each other.

CPU time slice

CPU time slice is the time allocated by CPU to each program, and each thread is assigned a time period, called its time slice.

Macroscopically, we can open multiple applications at the same time, each running in parallel and running at the same time.

But at the micro level: because there is only one CPU, can only deal with part of the program requirements at a time, how to deal with fairness, one way is to introduce time slices, each program takes turns to execute.

Parallel: vs Serial

Concurrency: a single core executes commands from multiple threads alternately

Thank you for your reading, the above is the content of "introduction and function of java program counter". After the study of this article, I believe you have a deeper understanding of the introduction and function of java program counter, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report