Home » Java programming language

Java Thread Class void setContextClassLoader(ClassLoader loader) method with Example

Java Thread Class void setContextClassLoader(ClassLoader loader) method: Here, we are going to learn about the void setContextClassLoader(ClassLoader loader) method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 31, 2019

Thread Class void setContextClassLoader(ClassLoader loader)

  • This method is available in package java.lang.Thread.setContextClassLoader(ClassLoader loader).
  • This method is used to sets the context ClassLoader for this(Current)Thread.
  • The context ClassLoader can be set at the time of thread creation by which thread creator could access the thread and provide appropriate the class loader to code run in the program at the time of class loading.
  • This method is not static so this method is accessible with Thread class object it is not accessible with the class name.
  • The return type of this method is void so it does not return anything.
  • This method raises an exception (SecurityException) if this thread could not set the context ClassLoader.

Syntax:

    void setContextClassLoader(ClassLoader loader){
    }

Parameter(s):

We pass only one object as a parameter in the method of the Thread and the parameter is the context ClassLoader for this thread.

Return value:

The return type of this method is void, it does not return anything.

Java program to demonstrate example of setContextClassLoader() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class SetContextClassLoader extends Thread {
    // Override run() of Thread class
    public void run() {
        //Display a message for the end user 	
        System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        // Creating an object of SetContextClassLoader class
        SetContextClassLoader ccl = new SetContextClassLoader();

        // Creating an object of Thread class
        Thread th = new Thread(ccl);

        // Thread class start() method will call and it will ultimately 
        th.start();

        // getContextClassLoader() will return context ClassLoader 
        // and create a reference of ClassLoader
        ClassLoader cl = th.getContextClassLoader();

        // By using setContextClassLoader(ClassLoader cl) sets 
        // the context ClassLoader for this thread th
        th.setContextClassLoader(cl);

        System.out.println("The Context ClassLoader for this thread th is = " + cl);
        System.out.println("The Parent of the ClassLoader is = " + cl.getParent());
        System.out.println("The Class of the ClassLoader is = " + cl.getClass());
    }
}

Output

E:\Programs>javac SetContextClassLoader.java

E:\Programs>java SetContextClassLoader
The Context ClassLoader for this thread th is = sun.misc.Launcher$AppClassLoader@33d626a4

The name of this thread is  Thread-1

The Parent of the ClassLoader is = sun.misc.Launcher$ExtClassLoader@3082f392

The Class of the ClassLoader is = class sun.misc.Launcher$AppClassLoader


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.