diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ClassLoaderTest.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ClassLoaderTest.java new file mode 100644 index 0000000..6c6c289 --- /dev/null +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ClassLoaderTest.java @@ -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 + */ \ No newline at end of file diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ClinitOrder.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ClinitOrder.java new file mode 100644 index 0000000..365a2e7 --- /dev/null +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ClinitOrder.java @@ -0,0 +1,27 @@ +package com.brianway.learning.java.jvm.classloading; + +/** + * Created by brian on 17/3/20. + * () 方法执行顺序 + */ +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); + } +} + + diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/ConstClass.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ConstClass.java similarity index 87% rename from java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/ConstClass.java rename to java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ConstClass.java index 3a6871a..9338623 100644 --- a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/ConstClass.java +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/ConstClass.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.jvm.classfile; +package com.brianway.learning.java.jvm.classloading; /** * Created by brian on 17/3/19. diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/DeadLoopClass.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/DeadLoopClass.java new file mode 100644 index 0000000..f2ec554 --- /dev/null +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/DeadLoopClass.java @@ -0,0 +1,30 @@ +package com.brianway.learning.java.jvm.classloading; + +/** + * Created by brian on 17/3/20. + * 字段解析 + * 多个线程同时初始化一个类,只有一个线程会执行 ()方法,其他被阻塞 + */ +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(); + } +} diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/FieldResolution.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/FieldResolution.java new file mode 100644 index 0000000..04ecfdb --- /dev/null +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/FieldResolution.java @@ -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); + } +} diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/NoInitialization.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/NoInitialization.java similarity index 91% rename from java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/NoInitialization.java rename to java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/NoInitialization.java index 3b0b749..4efbf0f 100644 --- a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/NoInitialization.java +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/NoInitialization.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.jvm.classfile; +package com.brianway.learning.java.jvm.classloading; /** * Created by brian on 17/3/19. diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SimpleClass.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SimpleClass.java similarity index 81% rename from java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SimpleClass.java rename to java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SimpleClass.java index 78cb7a2..fc00360 100644 --- a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SimpleClass.java +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SimpleClass.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.jvm.classfile; +package com.brianway.learning.java.jvm.classloading; /** * Created by brian on 17/3/15. diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SubClass.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SubClass.java similarity index 74% rename from java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SubClass.java rename to java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SubClass.java index a49b1c0..a15c620 100644 --- a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SubClass.java +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SubClass.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.jvm.classfile; +package com.brianway.learning.java.jvm.classloading; /** * Created by brian on 17/3/19. diff --git a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SuperClass.java b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SuperClass.java similarity index 83% rename from java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SuperClass.java rename to java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SuperClass.java index 49042f8..6b83c5c 100644 --- a/java-jvm/src/main/java/com/brianway/learning/java/jvm/classfile/SuperClass.java +++ b/java-jvm/src/main/java/com/brianway/learning/java/jvm/classloading/SuperClass.java @@ -1,4 +1,4 @@ -package com.brianway.learning.java.jvm.classfile; +package com.brianway.learning.java.jvm.classloading; /** * Created by brian on 17/3/19.