当前位置:网站首页>JVM (4): active and passive use of classes, internal structure of runtime data area, JVM thread description, PC register
JVM (4): active and passive use of classes, internal structure of runtime data area, JVM thread description, PC register
2022-06-11 04:16:00 【Prince you】
One 、 Active and passive use of classes
stay JVM The middle represents two class There are two necessary conditions for an object to be the same class :
- The complete class name of a class must be consistent , Include package name .
- Load the classLoader( finger classLoader Instance object ) It has to be the same .
let me put it another way , stay JVM in , Even if these two class objects (class object ) From the same source class file , Loaded by the same virtual machine , But just load their ClassLoader Instance objects are different , Then the two class objects are not equal .
JVM You must know whether a type is loaded by the boot loader or the user class loader . If a type is loaded by a user class loader , that JVM A reference to the class loader is saved in the method area as part of the type information . When resolving a reference from one type to another ,JVM You need to ensure that these two types of classloaders are the same .
Java The way the program uses classes is divided into : Active use and passive use . Active use , There are seven situations ::
- Create an instance of a class
- Accessing static variables of a class or interface , Or assign a value to the static variable
- Calling static methods of a class
- Reflection ( such as :class.forName ( "com.atguigu. Test") )
- Initializing a subclass of a class
- Java The class marked as the startup class when the virtual machine starts
- JDK 7 Start with dynamic language support :java. lang.invoke.MethodHandle The parsing result of the instance REF_getstatic、REF_putstatic、REF_invokestatic The class corresponding to the handle is not initialized , Then initialize
In addition to the above seven cases , Other uses Java The way of a class is seen as a passive use of a class , Will not cause class initialization .
Two 、 The internal structure of the runtime data area


Memory is a very important system resource , It's hard disk and CPU Middle warehouse and Bridge , It carries the real-time operation of the operating system and application program .JVM The memory layout defines Java Memory request during run 、 Distribute 、 Managed policies , To ensure the JVM Efficient and stable operation of . Different JVM There are some differences between memory partition and management mechanism . combination JVM Virtual machine specification , Let's talk about the classic JVM Memory layout .




Every JVM only one Runtime example . That is, runtime environment , Equivalent to the middle frame of the memory structure : Runtime environment .
3、 ... and 、JVM Thread description in
A thread is a running unit in a program .JVM Allows an application to have multiple threads executing in parallel .
stay Hotspot JVM in , Each thread is mapped directly to the local thread of the operating system . When one Java When the thread is ready to execute , At this time, a local thread of the operating system is also created .Java After thread execution is terminated , Local threads also recycle .
The operating system is responsible for scheduling all threads to any available CPU On . Once the local thread is initialized successfully , It's going to call Java In thread run () Method .
If you use jconsoe Or any debugging tool , You can see that there are many threads running in the background . These background threads do not include calls public static void main (string[]) Of main Threads and all this main A thread created by a thread itself .
These main background system threads are in Hotspot JVM There are mainly the following :
- Virtual machine threads : This kind of thread operation needs JVM The safety point will be reached . The reason these operations have to happen in different threads is that they all need JVM Reach safety point , So the pile doesn't change . The execution types of this thread include "stop-the-world" Garbage collection , Thread stack collection , Thread suspension and biased lock revocation .
- Periodic task thread : This thread is the embodiment of time cycle events ( Like interruptions ), They are generally used for scheduling execution of periodic operations .
- GC Threads : This kind of thread pair is in JVM There are different kinds of garbage collection behaviors that support .
- Compile thread : This thread compiles bytecode into local code at run time .
- Signal scheduling thread : This thread receives signals and sends them to JVM, Inside it, it's handled by calling the appropriate method .
Four 、 Program counter (PC register ) summary

JVM Program count register in (Program counter Register) in , 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 , It's not a physical register in a broad sense , Maybe translate it into pc Counter ( Or instruction counter ) It will be more appropriate ( Also known as program hook ), And it's not easy to cause some unnecessary misunderstandings .JVM Medium ec Registers are for physics Pc An abstract simulation of registers .

- It's a small piece of memory , I can almost ignore it . It's also 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 Instruction address ; perhaps , If it's execution native Method , The value is not specified (undefned) .
- 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 Nothing is specified in the virtual machine specification outOtMemoryError Area of situation .
PC Examples of the use of registers


solve PC Register two interview questions
problem 1:

problem 2:PC Why are registers set to thread private ?
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 .
because CPU Time slice wheel limit , Many threads are executing concurrently , Any definite moment , A core in a processor or multi-core processor , Only one instruction in a thread will be executed .
This inevitably leads to frequent interruptions or recoveries , How to make sure there is no difference ﹖ After each thread is created , Will produce their own program counter and stack frame , Program counters do not affect each other among threads .
CPU Time slice
CPU Time slice is 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 .
But at 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 .
边栏推荐
- MySql索引
- Market prospect analysis and Research Report of engraving laser in 2022
- D structure as index of multidimensional array
- 司马炎爷爷 告诉你什么叫做内卷!
- 基于FPGA的一维卷积神经网络CNN的实现(五)数据量化(附代码)
- Cloud broadcast alert, guanghetong helps intelligent camera to build a "river protection" drowning prevention system
- Guanghetong LTE Cat4 module l716 is upgraded to provide affordable and universal wireless applications for the IOT industry
- Safe and borderless, Guanghe tongdai 5g module +ai intelligent security solution shines at CPSE Expo
- Source insight 4.0 setting shortcut keys for comments and uncomments
- JVM(1):介绍、结构、运行和生命周期
猜你喜欢

JVM(3):类加载器分类、双亲委派机制

Explain in detail the structure and working principle of the crystal oscillator

数据类型的转换和条件控制语句

JVM(7):动态链接、方法的调用、四种方法调用指令区分非虚方法和虚方法、invokedynamic指令的使用

Matter protocol

JVM(4):类的主动使用与被动使用、运行时数据区域内部结构、JVM线程说明、PC寄存器

SSLStrip 终极版 —— location 劫持

A - Eddy's AC puzzle (C language)
![[network] socket programming](/img/df/2afc300bfc2dd319247a4b75ef7e2c.png)
[network] socket programming

2022 love analysis · privacy computing vendor panoramic report | love Analysis Report
随机推荐
Discussion on the development trend of remote power management unit (Intelligent PDU)
从初代播种到落地生花,5G商用三周年“催生万物”
SSLStrip 终极版 —— location 劫持
Market prospect analysis and Research Report of modular lithium ion battery in 2022
Possible problems with password retrieval function (supplementary)
Vulkan-官方示例解读-Shadows(光栅化)
[server data recovery] data recovery case of RAID5 crash of buddy storage
Use tool classes to read Excel files according to certain rules
Esp32 gattc configuration UUID
A Security Analysis Of Browser Extensions
Market prospect analysis and Research Report of seed laser in 2022
2022爱分析· 隐私计算厂商全景报告 | 爱分析报告
Implementation of one-dimensional convolutional neural network CNN based on FPGA (V) data quantization (with code)
Fundamentals of embedded audio processing
SQL注入关联分析
JVM(1):介绍、结构、运行和生命周期
Embedded basic interface UART
司马炎爷爷 告诉你什么叫做内卷!
A - Eddy's AC puzzle (C language)
Market prospect analysis and Research Report of Ethernet scanner in 2022