Loaders such as java basic study notes

  • 2020-05-05 11:13:01
  • OfStack

class loader

The java classloader dynamically loads the required classes in JVM at runtime. The java classloader is based on three mechanisms: delegation, visibility, and singleness.

You load the classpath.class files into memory, process them into bytecode, and that's what the classloader does.

The delegate mechanism refers to passing the request to load a class to the parent loader and then loading it if the parent loader cannot find or cannot load the class. The visibility mechanism refers to the fact that all classes loaded by the parent loader are visible to the quilt loader, but the parent class loader loaded by the child loader is invisible. The singleness mechanism means that a class can only be loaded once by the same loader.

default class loader

There are three default classloaders :

BootStrap ExtClassLoader AppClassLoader

The class loader is also an java class, while BootStrap is not. Verify code:


public class ClassLoaderTest {
  public static void main(String[] args) {
    System.out.println(System.class.getClassLoader());
  }
}

Output: null

If System.out.println (System.class.getClassLoader ().toString); , null pointer exception :


Exception in thread "main" java.lang.NullPointerException
  at com.iot.classloader.ClassLoaderTest.main(ClassLoaderTest.java:10)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

As you can see, the System class is loaded by the BootStrap class loader.

Class loader delegation mechanism

of the class loader tree

class loader

General order in which classes are loaded:

First of all, the class loader of the current thread loads the first class in the thread. If class A applies class B, the java virtual machine will load class B using the class loader of class A

custom classloader writing principles

API:

Class ClassLoader

Template method design pattern

Parent:

loadClass(process of class loading, template)
findClass is the classloading logic
that is overridden by subclasses and called by the loadClass method defineClass gets the class file converted to the bytecode

Subclass: overrides the findClass method

Example:

loadClass method source


protected Class<?> loadClass(String name, boolean resolve)
  throws ClassNotFoundException
{
  synchronized (getClassLoadingLock(name)) {
    // First, check if the class has already been loaded
    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) {
        // ClassNotFoundException thrown if class not found
        // from the non-null parent class loader
      }

      if (c == null) {
        // If still not found, then invoke findClass in order
        // to find the class.
        long t1 = System.nanoTime();
        c = findClass(name);

        // this is the defining class loader; record the stats
        sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
        sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
        sun.misc.PerfCounter.getFindClasses().increment();
      }
    }
    if (resolve) {
      resolveClass(c);
    }
    return c;
  }
}

Example in API documentation :


class NetworkClassLoader extends ClassLoader {
   String host;
   int port;

   public Class findClass(String name) {
     byte[] b = loadClassData(name);
     return defineClass(name, b, 0, b.length);
   }

   private byte[] loadClassData(String name) {
     // load the class data from the connection
     . . .
   }
 }


Related articles: