-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add] add examples of class loading in jvm module
- Loading branch information
Showing
9 changed files
with
134 additions
and
5 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ClassLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.brianway.learning.java.jvm.classloading; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by brian on 17/3/20. | ||
* 不同的类加载器对 instanceof 关键字运算结果的影响 | ||
*/ | ||
public class ClassLoaderTest { | ||
public static void main(String[] args) throws Exception { | ||
ClassLoader myLoader = new ClassLoader() { | ||
@Override | ||
public Class<?> loadClass(String name) throws ClassNotFoundException { | ||
try { | ||
String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class"; | ||
|
||
InputStream is = getClass().getResourceAsStream(fileName); | ||
if (is == null) { | ||
return super.loadClass(name); | ||
} | ||
byte[] b = new byte[is.available()]; | ||
is.read(b); | ||
return defineClass(name, b, 0, b.length); | ||
} catch (IOException e) { | ||
throw new ClassNotFoundException(name); | ||
} | ||
} | ||
}; | ||
|
||
Object obj = myLoader.loadClass("com.brianway.learning.java.jvm.classloading.ClassLoaderTest").newInstance(); | ||
System.out.println(obj.getClass()); | ||
System.out.println(obj instanceof com.brianway.learning.java.jvm.classloading.ClassLoaderTest); | ||
} | ||
} | ||
|
||
/* | ||
class com.brianway.learning.java.jvm.classloading.ClassLoaderTest | ||
false | ||
*/ |
27 changes: 27 additions & 0 deletions
27
java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ClinitOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.brianway.learning.java.jvm.classloading; | ||
|
||
/** | ||
* Created by brian on 17/3/20. | ||
* <clinit>() 方法执行顺序 | ||
*/ | ||
public class ClinitOrder { | ||
static class Parent { | ||
public static int A = 1; | ||
|
||
static { | ||
System.out.println("execute before A = 2, A now is " + A); | ||
A = 2; | ||
} | ||
} | ||
|
||
static class Sub extends Parent { | ||
public static int B = A; | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
System.out.println(Sub.B); | ||
} | ||
} | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...arning/java/jvm/classfile/ConstClass.java → ...ing/java/jvm/classloading/ConstClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/DeadLoopClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.brianway.learning.java.jvm.classloading; | ||
|
||
/** | ||
* Created by brian on 17/3/20. | ||
* 字段解析 | ||
* 多个线程同时初始化一个类,只有一个线程会执行 <clinit>()方法,其他被阻塞 | ||
*/ | ||
public class DeadLoopClass { | ||
static { | ||
if (true) { | ||
System.out.println(Thread.currentThread() + " init DeadLoopClass"); | ||
while (true) { | ||
|
||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
Runnable script = () -> { | ||
System.out.println(Thread.currentThread() + " start"); | ||
DeadLoopClass dlc = new DeadLoopClass(); | ||
System.out.println(Thread.currentThread() + " run over"); | ||
}; | ||
|
||
Thread thread1 = new Thread(script); | ||
Thread thread2 = new Thread(script); | ||
thread1.start(); | ||
thread2.start(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/FieldResolution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.brianway.learning.java.jvm.classloading; | ||
|
||
/** | ||
* Created by brian on 17/3/20. | ||
* 字段解析 | ||
* 如果注释了 Sub 类中的 "public static int A = 4",会编译错误 | ||
*/ | ||
public class FieldResolution { | ||
interface Interface0 { | ||
int A = 0; | ||
} | ||
|
||
interface Interface1 extends Interface0 { | ||
int A = 1; | ||
} | ||
|
||
interface Interface2 { | ||
int A = 2; | ||
} | ||
|
||
static class Parent implements Interface1 { | ||
public static int A = 3; | ||
} | ||
|
||
static class Sub extends Parent implements Interface2 { | ||
public static int A = 4;// 注释此句试试 | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(Sub.A); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../java/jvm/classfile/NoInitialization.java → ...va/jvm/classloading/NoInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rning/java/jvm/classfile/SimpleClass.java → ...ng/java/jvm/classloading/SimpleClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...learning/java/jvm/classfile/SubClass.java → ...rning/java/jvm/classloading/SubClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...arning/java/jvm/classfile/SuperClass.java → ...ing/java/jvm/classloading/SuperClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters