public class Launcher {
private static URLStreamHandlerFactory factory = new Launcher. Factory ( ) ;
private static Launcher launcher = new Launcher ( ) ;
private static String bootClassPath = System . getProperty ( "sun.boot.class.path" ) ;
private ClassLoader loader;
private static URLStreamHandler fileHandler;
public static Launcher getLauncher ( ) {
return launcher;
}
public Launcher ( ) {
Launcher. ExtClassLoader var1;
try {
var1 = Launcher. ExtClassLoader . getExtClassLoader ( ) ;
} catch ( IOException var10) {
throw new InternalError ( "Could not create extension class loader" , var10) ;
}
try {
this . loader = Launcher. AppClassLoader . getAppClassLoader ( var1) ;
} catch ( IOException var9) {
throw new InternalError ( "Could not create application class loader" , var9) ;
}
Thread . currentThread ( ) . setContextClassLoader ( this . loader) ;
}
static class ExtClassLoader extends URLClassLoader {
public static Launcher. ExtClassLoader getExtClassLoader ( ) throws IOException {
final File [ ] var0 = getExtDirs ( ) ;
try {
return ( Launcher. ExtClassLoader ) AccessController . doPrivileged ( new PrivilegedExceptionAction < Launcher. ExtClassLoader > ( ) {
public Launcher. ExtClassLoader run ( ) throws IOException {
int var1 = var0. length;
for ( int var2 = 0 ; var2 < var1; ++ var2) {
MetaIndex . registerDirectory ( var0[ var2] ) ;
}
return new Launcher. ExtClassLoader ( var0) ;
}
} ) ;
} catch ( PrivilegedActionException var2) {
throw ( IOException ) var2. getException ( ) ;
}
}
static class AppClassLoader extends URLClassLoader {
final URLClassPath ucp = SharedSecrets . getJavaNetAccess ( ) . getURLClassPath ( this ) ;
public static ClassLoader getAppClassLoader ( final ClassLoader var0) throws IOException {
final String var1 = System . getProperty ( "java.class.path" ) ;
final File [ ] var2 = var1 == null ? new File [ 0 ] : Launcher . getClassPath ( var1) ;
return ( ClassLoader ) AccessController . doPrivileged ( new PrivilegedAction < Launcher. AppClassLoader > ( ) {
public Launcher. AppClassLoader run ( ) {
URL[ ] var1x = var1 == null ? new URL[ 0 ] : Launcher . pathToURLs ( var2) ;
return new Launcher. AppClassLoader ( var1x, var0) ;
}
} ) ;
}
}
public abstract class ClassLoader {
private final ClassLoader parent;
protected Class < ? > loadClass ( String name, boolean resolve)
throws ClassNotFoundException
{
synchronized ( getClassLoadingLock ( name) ) {
Class < ? > c = findLoadedClass ( name) ;
if ( c == null ) {
long t0 = System . nanoTime ( ) ;
try {
if ( parent != null ) {
c = parent. loadClass ( name, false ) ;
} else {
c = findBootstrapClassOrNull ( name) ;
}
} catch ( ClassNotFoundException e) {
}
if ( c == null ) {
long t1 = System . nanoTime ( ) ;
c = findClass ( name) ;
}
}
if ( resolve) {
resolveClass ( c) ;
}
return c;
}
}
protected Object getClassLoadingLock ( String className) {
Object lock = this ;
if ( parallelLockMap != null ) {
Object newLock = new Object ( ) ;
lock = parallelLockMap. putIfAbsent ( className, newLock) ;
if ( lock == null ) {
lock = newLock;
}
}
return lock;
}
总结 1.双亲委派机制的实现原理 2.并发编程synchronized关键字保证类只被加载一次 3.缓存的使用 4.线程上下文