JVM 类的生命周期、类加载器
类的加载、连接与初始化
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Test { public static void main(String[] args) { Count count = Count.getInstance(); System.out.println("count1 = " + count.count1); System.out.println("count2 = " + count.count2); }}class Count { // 这个运行结果是 count1 = 1 count2 = 0 ; 因为按顺序执行1. Count(); 2. count1; 3. count2; private static Count count = new Count(); public static int count1; public static int count2 = 0; // 所以这个运行结果是 count1 = 1 count2 = 1 ; // private static Count count = new Count(); private Count() { count1++; count2++; } public static Count getInstance() { return count; }} |
1. 类的加载
2. 连接
2.1 类的验证
2.2 类的准备
|
1
2
3
4
5
6
7
8
9
|
public class Sample { private static int a = 1; private static long b; static { b = 2; } // ...} |
2.3 类的解析
|
1
2
3
|
public void gotoWord(){ car.run(); //这段代码在Worker类的二进制数据中表示为符号引用} |
3. 类的初始化
|
1
2
3
4
5
6
7
8
9
10
|
public class Sample { private static int a = 1; private static long b; private static long c; static { b = 2; } // ...} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class Sample { private static int a = 1; static { a = 2; } static { a = 4; } public static void main(String[] args) { System.out.println(a); // 输出4 }} |
3. 1 类的初始化时机
以下代码可以加深理解:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Test { public static void main(String[] args) { // x是一个编译时的常量,编译的时候就知道值是多少,不需要对类进行初始化 System.out.println(FinalTest.x); // x非编译时的常量,x在编译时不知道是多少, // 运行才知道的就需要对类进行初始化,对类进行初始化static代码快就会执行 System.out.println(FinalTest2.x); }}class FinalTest { public static final int x = 6 / 3; static { System.out.println("FinalTest staic block!"); }}class FinalTest2 { public static final int x = new Random().nextInt(100); static { System.out.println("FinalTest2 staic block!"); }} |
运行结果:
当 JVM 初始化一个类时,要求它的所有父类都己经被初始化,但是这条规则并不适用于接口。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Test { static { System.out.println("Test static block!"); } public static void main(String[] args) { System.out.println(Child.b); }}class Parent { static int a = 3; static { System.out.println("Parent static block!"); }}class Child extends Parent { static int b = 4; static { System.out.println("Child static block!"); }} |
运行结果:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Test { public static void main(String[] args) { System.out.println(Child.a); Child.doSomething(); }}class Parent { static int a = 3; static { System.out.println("Parent static block!"); } static void doSomething() { System.out.println("do something!"); }}class Child extends Parent { static { System.out.println("Child static block!"); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Test { public static void main(String[] args) throws ClassNotFoundException { // 获取系统类加载器 ClassLoader loader = ClassLoader.getSystemClassLoader(); // 这行代码没有导致任何输出 不会导致类的初始化 Class<?> clazz = loader.loadClass("CL"); System.out.println("------"); clazz = Class.forName("CL"); }}class CL { static { System.out.println("Class CL"); }} |
类加载器
扩展(Extension)类加载器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Test { public static void main(String[] args) throws ClassNotFoundException { // String 是由根类加载器加载的,下面打印结果为null Class<?> clazz = Class.forName("java.lang.String"); System.out.println(clazz.getClassLoader()); // 应用加载器加载的 Class<?> clazz2 = Class.forName("C"); System.out.println(clazz2.getClassLoader()); }}class C {} |
类加载的父委托机制
|
1
2
3
4
|
ClassLoader loader1 = new MyClassLoader();// 参数loader1将作为loader2的父加载器ClassLoader loader2 = new MyClassLoader(loader1); |
命名空间
每个类加载器都有自己的命名空间,命名空间由该加载器及所有父加载器所加载的类组成。在同一个命名空间中,不会出现类的完整名字(包括类的包名)相同的两个类;在不同的命名空间中,有可能会出现类的完整名字(包括类的包名)相同的两个类。
运行时包(package)
创建用户自定义的类加载器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
public class MyClassLoader extends ClassLoader { // 类加载器名字 private String name; // 加载类的路径 private String path = "d:\\"; // class文件的扩展名 private final String fileType = ".class"; public MyClassLoader(String name) { super();// 让系统类加载器成为该类加载器的父加载器 this.name = name; } public MyClassLoader(ClassLoader parent, String name) { super(parent); // 显示指定该类加载器的父加载器 this.name = name; } public String toString() { return this.name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { byte[] data = this.loadClassData(name); return this.defineClass(name, data, 0, data.length); } private byte[] loadClassData(String name) { InputStream is = null; byte[] data = null; ByteArrayOutputStream baos = null; try { this.name = this.name.replace(".", "\\"); is = new FileInputStream(new File(path + name + fileType)); baos = new ByteArrayOutputStream(); int ch = 0; while (-1 != (ch = is.read())) { baos.write(ch); } data = baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { baos.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } return data; } public static void main(String[] args) throws Exception { // 父加载器为系统类加载器 MyClassLoader loader1 = new MyClassLoader("loader1"); loader1.setPath("D:\\test\\serverlib\\"); // 指定loader2的父加载器为loader1 MyClassLoader loader2 = new MyClassLoader(loader1, "loader2"); loader2.setPath("D:\\test\\clientlib\\"); // 指定loader3的父加载器为根加载器 MyClassLoader loader3 = new MyClassLoader(null, "loader3"); loader3.setPath("D:\\test\\otherlib\\"); test(loader2); test(loader3); } public static void test(ClassLoader loader) throws Exception { Class clazz = loader.loadClass("Sample"); Object object = clazz.newInstance(); }} |
|
1
2
3
4
5
6
|
public class Dog { public Dog() { System.out.println("Dog is load by : " + this.getClass().getClassLoader()); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Sample { public int v1 = 1; public Sample() { System.out.println("Sample is load by : " + this.getClass().getClassLoader()); // 主动使用Dog new Dog(); }} |
|
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoaderSample is load by : loader1Dog is load by : loader1Sample is load by : loader3Dog is load by : loader3 |
|
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoaderSample is load by : sun.misc.Launcher$AppClassLoader@659e0bfdDog is load by : sun.misc.Launcher$AppClassLoader@659e0bfdSample is load by : loader3Dog is load by : loader3 |
|
1
2
3
4
5
|
D:\myapp\syslib>java -cp .;d:\myapp\serverlib MyClassLoaderSample is load by : sun.misc.Launcher$AppClassLoader@659e0bfdDog is load by : sun.misc.Launcher$AppClassLoader@659e0bfdSample is load by : loader3Dog is load by : loader3 |
|
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoaderSample is load by : loader1Dog is load by : sun.misc.Launcher$AppClassLoader@659e0bfdSample is load by : loader3Dog is load by : loader3 |
|
1
2
3
4
5
6
7
8
9
10
11
|
public static void main(String[] args) throws Exception { // 父加载器为系统类加载器 MyClassLoader loader1 = new MyClassLoader("loader1"); loader1.setPath("D:\\myapp\\serverlib\\"); Class clazz = loader1.loadClass("Sample"); Object object = clazz.newInstance(); // 创建对象 Sample sample = (Sample)object; System.out.println(sample.v1);} |
|
1
2
3
4
5
6
7
8
9
10
11
|
D:\myapp\syslib>java MyClassLoaderSample is load by : loader1Dog is load by : loader1Exception in thread "main" java.lang.NoClassDefFoundError: Sample at MyClassLoader.main(MyClassLoader.java:110)Caused by: java.lang.ClassNotFoundException: Sample at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public static void main(String[] args) throws Exception { // 父加载器为系统类加载器 MyClassLoader loader1 = new MyClassLoader("loader1"); loader1.setPath("D:\\myapp\\serverlib\\"); Class clazz = loader1.loadClass("Sample"); Object object = clazz.newInstance(); // 创建对象 Field field = clazz.getField("v1"); int v1 = field.getInt(object); System.out.println("v1:" + v1);} |
|
1
2
3
4
|
D:\myapp\syslib>java MyClassLoaderSample is load by : loader1Dog is load by : loader1v1:1 |
类的卸载
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public static void main(String[] args) throws Exception { // 父加载器为系统类加载器 MyClassLoader loader1 = new MyClassLoader("loader1"); //1 loader1.setPath("D:\\myapp\\serverlib\\"); //2 Class objClass = loader1.loadClass("Sample"); //3 System.out.println("objClass's hashCode is " + objClass.hashCode()); //4 Object obj = objClass.newInstance(); // 创建对象 //5 loader1 = null; //6 objClass = null; //7 obj = null; //8 loader1 = new MyClassLoader("loader1"); //9 objClass = loader1.loadClass("Sample"); //10 System.out.println("objClass's hashCode is " + objClass.hashCode()); //11} |
|
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoaderobjClass's hashCode is 1311053135Sample is load by : loader1Dog is load by : loader1objClass's hashCode is 865113938 |
JVM 类的生命周期、类加载器的更多相关文章
- Java - JVM - 类的生命周期
概述 简述 JVM 里 类的生命周期 上次写了 30%, 居然丢了 难受, 又要重新写 类的生命周期 加载 使用 卸载 1. 加载 概述 类型的加载 大体流程 装载 连接 验证 准备 解析(可选的) ...
- JVM类加载器及Java类的生命周期
预定义类加载器(三种): 启动(Bootstrap)类加载器: 是用本地代码实现的类装入器,它负责将<Java_Runtime_Home>/lib下面的类库加载到内存中(比如rt.jar) ...
- 乐字节Java反射之三:方法、数组、类加载器和类的生命周期
本文承接上一篇:乐字节Java发射之二:实例化对象.接口与父类.修饰符和属性 继续讲述Java反射之三:方法.数组.类加载器 一.方法 获取所有方法(包括父类或接口),使用Method即可. publ ...
- JVM:类的生命周期
类的生命周期 综述 1. 只有当一个类被切实使用到的时候才会被加载到虚拟机中(例如:new, 方法调用, A a = null;不算) 2. 若在加载一个类的过程中,有其他类被切实使用到, ...
- <JVM中篇:字节码与类的加载篇>03-类的加载过程(类的生命周期)详解
笔记来源:尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机) 同步更新:https://gitee.com/vectorx/NOTE_JVM https://codechina.cs ...
- JVM与垃圾回收机制(GC)和类的生命周期
JVM运行时数据区 GC(垃圾回收机制) 什么是垃圾回收机制: 在系统运行过程中,会产生一些无用的对象,这些对象占据着一定的内存,如果不对这些对象清理回收无用的是对象,可能会导致内存的耗尽,所以垃圾回 ...
- Java类的生命周期详解
引言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑,笔者打开百度搜了一下相关的问题,看到网上的资料很少有把这个问题讲明白的,主要是因为目前国内java方面的教材大多只是告 ...
- 【转】Java 类的生命周期详解
一. 引 言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑,笔者打开百度搜了一下相关的问题,看到网上的资料很少有把这个问题讲明白的,主要是因为目前国内java方面的教材大 ...
- 【转载】详解java类的生命周期
原文地址:http://blog.csdn.net/zhengzhb/article/details/7517213 引言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑, ...
随机推荐
- Spring Boot+maven打war包
存在一个坑: 官网文档 指出以下前3步做法,但是这样只可以打出可运行的jar包,要打出war包还要在文档后面的链接跳到另一个页面,才能找到第四步的做法,也就是最终能够打出war包,可能有些朋友有些粗心 ...
- Spark Streaming应用启动过程分析
本文为SparkStreaming源码剖析的第三篇,主要分析SparkStreaming启动过程. 在调用StreamingContext.start方法后,进入JobScheduler.start方 ...
- JavaSE基础问答
1.JAVA的基本数据类型有哪些? JAVA的基本数据类型分为4类8种,就是整形 byte.short.int.long.浮点型 float 跟double,字符型 char,跟布尔型 true和 f ...
- IntelliJ IDEA在Local模式下Spark程序消除日志中INFO输出
在使用Intellij IDEA,local模式下运行Spark程序时,会在Run窗口打印出很多INFO信息,辅助信息太多可能会将有用的信息掩盖掉.如下所示 要解决这个问题,主要是要正确设置好log4 ...
- 常用的DDL语句
create database mydb1; 创建一个名称为mydb1的数据库. use db_name; 切换数据库 ; show databases; 查看所有的数据库: select datab ...
- 用Python递归解决阿拉伯数字转为中文财务数字格式的问题(2)--打开思路的一种方法
几天前自己写了个将阿拉伯数字转为中文财务数字的程序.用的递归,不幸的是它是树形递归. 虽然实际过程中不太可能出现金额数字大到让Python递归栈溢出,但是始终是一块心病,这玩意终究在理论上是受限制的. ...
- JAVA面向对象-----匿名内部类
匿名内部类 匿名内部类:就是没有类名字的内部类. 匿名内部类作用:简化内部类书写. 匿名内部类的前提:必须继承一个父类或者是实现一个接口. 匿名内部类的格式: new 父类或者接口(){ 执行代码-. ...
- Apache shiro集群实现 (八) web集群时session同步的3种方法
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- Linux 高性能服务器编程——socket选项
socket选项函数 功能:用来读取和设置socket文件描述符属性的方法 函数: #include <sys/scoket.h> int getsockopt ( int sockfd, ...
- Linux 高性能服务器编程——TCP协议详解
问题聚焦: 本节从如下四个方面讨论TCP协议: TCP头部信息:指定通信的源端端口号.目的端端口号.管理TCP连接,控制两个方向的数据流 TCP状态转移过程:TCP连接的任意一 ...