开启类加载日志
-verbose:class,如java -verbose:class xxx,就会开启了jvm加载类的日志输出,例如观察依赖类加载是线程安全的特性来实现延迟加载的单例:
public class Singleton {
private Singleton() {}
private static class LazyHolder {
static final Singleton INSTANCE = new Singleton();
static {
System.out.println("LazyHolder.<clinit>");
}
}
public static Object getInstance(boolean flag) {
if (flag) return new LazyHolder[2];
return LazyHolder.INSTANCE;
}
public static void main(String[] args) {
getInstance(true);
System.out.println("----");
getInstance(false);
}
}
运行: java -verbose:class Singleton,输出如下log:
[Opened /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.Object from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.io.Serializable from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.Comparable from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.CharSequence from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.String from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.reflect.AnnotatedElement from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.reflect.GenericDeclaration from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.reflect.Type from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.Class from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
.
.
.
[Loaded java.security.BasicPermissionCollection from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded Singleton from file:/home/mcloud/test/]
[Loaded sun.launcher.LauncherHelper$FXHelper from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.Class$MethodArray from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.Void from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded Singleton$LazyHolder from file:/home/mcloud/test/]
----
LazyHolder.<clinit>
[Loaded java.lang.Shutdown from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /usr/lib/jvm/jdk1.8.0_92/jre/lib/rt.jar]
网友评论