Skip to content

Commit

Permalink
[add] add examples of class loading in jvm module
Browse files Browse the repository at this point in the history
  • Loading branch information
brianway committed Mar 20, 2017
1 parent 6840e59 commit 90bfef7
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 5 deletions.
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
*/
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);
}
}


Original file line number Diff line number Diff line change
@@ -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.
Expand Down
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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 90bfef7

Please sign in to comment.