jvm prepared from the java program executed by the link to the structure
1.java java front end compiler is responsible for compiling the source code into bytecodes -> compiler front end
2.java virtual machine is responsible for loading bytecode compiled into the interior -> the step of loading the operating region and class java
Class load: loaded, link initialization
Class loader
Parent delegation model
Parent delegation model (Parent Delegation Model):
Loading class takes parents entrust mechanism that can better ensure the safety of the Java platform.
This model requires addition top Bootstrap class loader boot class loader, the rest of the class loader should have its own 父类加载器
. Child and parent class loader class loader 不是以继承(Inheritance)的关系
is achieved, but by 组合(Composition)关系
multiplexed codes with parent loader. Each class loader has its own namespace (of the loader and all the parent class loader loaded classes in the same namespace, class name does not appear complete (including package of) the same the two classes; in a different namespace, there may be a full class name (including package of) two of the same class)
Parents can delegate process model is as follows:
1. Does the current ClassLoader first queries from his already loaded class such has been loaded, if already loaded directly back to the original already loaded class.
每个类加载器都有自己的加载缓存,当一个类被加载了以后就会放入缓存, 等下次加载的时候就可以直接返回了。
2. Current classLoader not found in the cache loaded class, commissioned by the parent class loader to load, the parent class loader using the same strategy, first check its cache, then delegate the parent class of the parent class to load, has been to ClassLoader on Bootstrap.
3. when all the parent class loader are not loaded, and then loaded by the current class loader, and placed in its own cache, so next time there is a load request return directly.
The benefits of using this model to organize the relationship between the class loader:
mainly to 安全性
avoid user-written Java class dynamically replace some core classes, such as String, but also to avoid 重复加载
, because the JVM to distinguish between different classes, not only It is just loaded two different classes based on the class name, the same class file different ClassLoader, if mutual transformation, then will throw java.lang.ClassCaseException.
Custom class loader
3. Finally interpreted / compiled into machine instructions executed on the corresponding platform -> jvm execution engine and the garbage collection GC
Java bytecode is executed in two ways:
1. in-time compiler manner: first interpreter bytecode compiler into machine code, and then executing the machine code.
2. interpretive execution mode: by an interpreter interprets and executes each piece of code to perform all operations of Java bytecode programs.
jdk and openjdk hotspot vm is in default comes with a virtual machine. Within the hotspot vm, in-time compiler and interpreter co-exist. This is because when the virtual machine is powered on, the interpreter can do their work first, without waiting for the compiler to compile all complete before execution, so can save a lot of unnecessary compilation time. Over and program run time, the compiler to function progressively, hot detection function according to the value (frequently called methods) byte code compiled to native machine instructions in exchange for a higher efficiency of program execution.