Home » Java programming language

Java Thread Class ClassLoader getContextClassLoader() method with Example

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

Thread Class ClassLoader getContextClassLoader()

  • This method is available in package java.lang.Thread.getContextClassLoader().
  • This method is used to return a context ClassLoader for this (Current) Thread.
  • 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 ClassLoader so it returns the context ClassLoader.
  • This method raises an exception (SecurityException) if this thread did not get the context ClassLoader.

Syntax:

    ClassLoader getContextClassLoader(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is ClassLoader, it returns the context ClassLoader.

Java program to demonstrate example of getContextClassLoader() 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 ContextClassLoader 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 ContextClassLoader class
        ContextClassLoader ccl = new ContextClassLoader();

        // 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 ContextClassLoader.java

E:\Programs>java ContextClassLoader
The Context ClassLoader for this thread th is = [email protected]

The name of this thread is  Thread-1

The Parent of the ClassLoader is = [email protected]

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.