1, 共5个必备线程,和1个用户线程Thread[Thread-0,5,main] //[name,priority,group.name]
main group: 5-main; 5-Thread-0
system group: 10-Reference Handler, 9-Signal Dispatcher, 8-Finalizer, 5-Attach Listener
2,共2个ClassLoader:
Bootstrap classloader是C写的,故==null, 加载10-Reference Handler, 8-Finalizer
AppClassLoader,
/**
* output :
* Thread[Thread-0,5,main]====sun.misc.Launcher$AppClassLoader@5984cf08
* Thread[main,5,main]====sun.misc.Launcher$AppClassLoader@5984cf08
* Thread[Finalizer,8,system]====null [Bootstrap classloader]
* Thread[Thread-0,5,main]====sun.misc.Launcher$AppClassLoader@5984cf08
* Thread[Reference Handler,10,system]====null [Bootstrap classloader]
* Thread[Signal Dispatcher,9,system]====sun.misc.Launcher$AppClassLoader@5984cf08
* Thread[Attach Listener,5,system]====sun.misc.Launcher$AppClassLoader@5984cf08
* @author naijiang.wang
* @date 2013-08-05 9:37:22 AM
*/
public class T {
public static void main( String[] args )
throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
Thread t0 = Thread.currentThread();
System.out.println(t0+"===="+t0.getContextClassLoader());
try {
Thread.sleep( 1000 );
}
catch ( InterruptedException e ) {
e.printStackTrace();
}
}
}).start();
for(Thread t : Thread.getAllStackTraces().keySet()){
System.out.println(t+"===="+t.getContextClassLoader());
}
}
}
2,自定义java.lang package,会导致jvm错误:
关键:可以看出加载类顺序
package java.lang;
/**
* output:
* Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.lang
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:649)
at java.lang.ClassLoader.defineClass(ClassLoader.java:785)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)
* @author rivertime.wang
* @date 2014-3-8 15:06:38
*/
public class TestClassLoader {
public static void main(String[] args) {
System.out.println("user define 'java.lang' package");
}
}
3,bootstrap, sun.misc.Launcher$AppClassLoader, ExtClassLoader
public class Test {
public static void main( String[] args )
throws IOException {
System.out.println( System.getProperty( "java.class.path" ) );
// null,bootstrap classloader(不是ClassLoader实例)
System.out.println( System.class.getClassLoader() );
// sun.misc.Launcher$AppClassLoader@4089f3e5
System.out.println( ClassLoader.getSystemClassLoader() );
System.out.println( Test.class.getClassLoader() );
// sun.misc.Launcher$ExtClassLoader@20eb607d
System.out.println( ClassLoader.getSystemClassLoader().getParent() );
System.out.println( Test.class.getClassLoader().getParent() );
}
}