jvm Classload method介绍
生活随笔
收集整理的這篇文章主要介紹了
jvm Classload method介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1,jvm Classload默認幾個重要方法介紹
findClass:Finds and loads the class with the specified name from the URL search path.找到class文件并把字節碼載入到內存中,假設自己定義的載入器僅覆蓋了findClass,而未覆蓋loadClass(即載入規則一樣,但載入路徑不同);則調用getClass().getClassLoader()返回的仍然是AppClassLoader!由于真正load類的,還是AppClassLoader
defineClass:Converts an array of bytes into an instance of class. 創建類對象,將字節流解析成JVM可以識別的Class對象
loadClass:Loads the class with the specified binary name.自己定義的載入器能夠覆蓋該方法loadClass(),以便定義不同的載入機制.比如Servlet中的WebappClassLoader覆蓋了該方法,在WEB-INFO/classes文件夾下查找類文件;在載入時,假設成功,則緩存到ResourceEntry對象。——不同的載入機制。
2,其他一些方法
resolveClass:載入完字節碼后,會依據須要進行驗證、解析
defineClass:字節流解析成JVM可以識別的Class對象。不可覆蓋
findLoadedClass:假設類已經載入過,則直接返回 Returns the class with the given name binary name if this loader has been recorded by the Java virtual machine as an initiating
3,調用規則:loadClass-->findClass-->defineClass--->resolveClass(可選)
4,JVM默認不能熱部署類,由于載入類時會去調用findLoadedClass(),假設類已被載入,就不會再次載入。JVM推斷類是否被載入有兩個條件:完整類名是否一樣、ClassLoader是否是同一個。
loadClass方法代碼:
??? protected Class<?> loadClass(String name, boolean resolve)throws ClassNotFoundException{synchronized (getClassLoadingLock(name)) {// First, check if the class has already been loadedClass c = findLoadedClass(name);if (c == null) {long t0 = System.nanoTime();try {if (parent != null) {c = parent.loadClass(name, false);} else {c = findBootstrapClassOrNull(name);}} catch (ClassNotFoundException e) {// ClassNotFoundException thrown if class not found// from the non-null parent class loader}if (c == null) {// If still not found, then invoke findClass in order// to find the class.long t1 = System.nanoTime();c = findClass(name);// this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);sun.misc.PerfCounter.getFindClasses().increment();}}if (resolve) {resolveClass(c);}return c;}}
findClass:Finds and loads the class with the specified name from the URL search path.找到class文件并把字節碼載入到內存中,假設自己定義的載入器僅覆蓋了findClass,而未覆蓋loadClass(即載入規則一樣,但載入路徑不同);則調用getClass().getClassLoader()返回的仍然是AppClassLoader!由于真正load類的,還是AppClassLoader
defineClass:Converts an array of bytes into an instance of class. 創建類對象,將字節流解析成JVM可以識別的Class對象
loadClass:Loads the class with the specified binary name.自己定義的載入器能夠覆蓋該方法loadClass(),以便定義不同的載入機制.比如Servlet中的WebappClassLoader覆蓋了該方法,在WEB-INFO/classes文件夾下查找類文件;在載入時,假設成功,則緩存到ResourceEntry對象。——不同的載入機制。
2,其他一些方法
resolveClass:載入完字節碼后,會依據須要進行驗證、解析
defineClass:字節流解析成JVM可以識別的Class對象。不可覆蓋
findLoadedClass:假設類已經載入過,則直接返回 Returns the class with the given name binary name if this loader has been recorded by the Java virtual machine as an initiating
3,調用規則:loadClass-->findClass-->defineClass--->resolveClass(可選)
4,JVM默認不能熱部署類,由于載入類時會去調用findLoadedClass(),假設類已被載入,就不會再次載入。JVM推斷類是否被載入有兩個條件:完整類名是否一樣、ClassLoader是否是同一個。
loadClass方法代碼:
??? protected Class<?> loadClass(String name, boolean resolve)throws ClassNotFoundException{synchronized (getClassLoadingLock(name)) {// First, check if the class has already been loadedClass c = findLoadedClass(name);if (c == null) {long t0 = System.nanoTime();try {if (parent != null) {c = parent.loadClass(name, false);} else {c = findBootstrapClassOrNull(name);}} catch (ClassNotFoundException e) {// ClassNotFoundException thrown if class not found// from the non-null parent class loader}if (c == null) {// If still not found, then invoke findClass in order// to find the class.long t1 = System.nanoTime();c = findClass(name);// this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);sun.misc.PerfCounter.getFindClasses().increment();}}if (resolve) {resolveClass(c);}return c;}}
總結
以上是生活随笔為你收集整理的jvm Classload method介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 望故乡
- 下一篇: iOS开发网络篇—网络请求(HTTP协议