Java Data Type Tutorial - Java Thread .getContextClassLoader ()








Syntax

Thread.getContextClassLoader() has the following syntax.

public ClassLoader getContextClassLoader()

Example

In the following code shows how to use Thread.getContextClassLoader() method.

//  ww  w . j  a  v  a2s.co  m

class ThreadDemo extends Thread {

  public void run() {

    // returns the context ClassLoader for this Thread
    ClassLoader c = getContextClassLoader();

    // sets the context ClassLoader for this Thread
    setContextClassLoader(c);
    System.out.println("Class = " + c.getClass());
    System.out.println("Parent = " + c.getParent());
  }

}

public class Main {

  public static void main(String args[]) {

    ThreadDemo t = new ThreadDemo();
    // this will call run() function
    t.start();
  }
}

The code above generates the following result.