当前位置:网站首页>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 .

原网站

版权声明
本文为[Prince you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110405099721.html