Java Basic Learning notes and the like loader _java

Source: Internet
Author: User

Class Loader

Java class loaders are dynamically loading the required classes in the JVM at run time, and the Java class loader is based on three mechanisms: delegate, visible, single.

Load the. class files under the classpath into memory and become bytecode after processing, which is done by the ClassLoader.

    1. A delegate mechanism refers to passing a request for an add-in class to the parent loader, and then loading it if the parent loader cannot find or load the class.
    2. The visibility mechanism refers to classes that are loaded by the parent loader that can be seen by the quilt loader, but the class parent loader loaded by the child loader is invisible.
    3. A single mechanism refers to a class that can only be loaded once by the same loader.

Default class Loader

System default three class loaders:

    1. BootStrap
    2. Extclassloader
    3. Appclassloader

The class loader is also a Java class, and Bootstrap is not. Validation 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) is used, a null pointer exception is reported:

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)

Visible, the system class is loaded by the Bootstrap class loader.

delegate mechanism of class loader

Tree diagram of class loader

Class Loader

The order of the general load classes:

    1. The class loader of the current thread first loads the first class in the thread
    2. If Class A applies a class B,java virtual machine will load Class B using the class loader of load Class A
    3. You can also call the Classloader.loadclass () method directly to develop a class loader to load a class

The principle of writing a custom class loader

Api:

Class ClassLoader

Template Method Design Pattern

Parent class:

LoadClass (class-loaded process, template)
Findclass class load logic that is called by the LoadClass method for child class coverage
DefineClass get class file to convert to byte code

Subclasses: Overriding Findclass methods

Example:

The source code of the LoadClass method

Protected class<?> loadclass (String name, Boolean resolve) throws ClassNotFoundException {synchronized (GETCLA Ssloadinglock (name)) {//I, check if the class has already been loaded class<?> C = findloadedclass (nam
    e);
      if (c = = null) {Long T0 = System.nanotime ();
        try {if (parent!= null) {c = Parent.loadclass (name, false);
        else {c = findbootstrapclassornull (name); The catch (ClassNotFoundException e) {//ClassNotFoundException thrown if class not found//from T He non-null parent class loader} if (c = null) {//If still not found, then invoke Findclass in or
        Der//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;

 }
}

Examples in the 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 ...
   }
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.