Thread getContextClassLoader() in Java

getContextClassLoader(): This method is available in the java.lang.Thread class of Java.

Syntax:

ClassLoader java.lang.Thread.getContextClassLoader()

This method returns the context ClassLoader for this thread.

The context ClassLoader is provided by the creator of the thread for use by code running in this thread when loading classes and resources. If not set, the default is the ClassLoader context of the parent thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load the application.

Parameters: NA

Returns: the context ClassLoader for this thread, or null indicating the system class loader.

Throws:

1. SecurityException - if a security manager is present, and the caller's class loader is not null and is not the same as or an ancestor of the context class loader, and the caller does not have the RuntimePermission("getClassLoader").

Approach

Java

package com.Thread;

public class ThreadgetContextClassLoader {
    public static void main(String[] args) {

        Thread thread = new Thread();

        System.out.println(thread.getContextClassLoader());
    }
}

Output:

jdk.internal.loader.ClassLoaders$AppClassLoader@443b7951


Some other methods of Thread

Thread()This method allocates a new Thread object.

Thread(Runnable)This method allocates a new Thread object with one argument.

Thread(String)This method allocates a new Thread object with the specified name.

Thread(Runnable, String)This method allocates a new Thread object with the specified runnable and the specified name.

Thread(ThreadGroup, Runnable)This method allocates a new Thread object with the specified ThreadGroup and the specified runnable.

Thread(ThreadGroup, String)This method allocates a new Thread object with the specified ThreadGroup and the specified name.

Thread(ThreadGroup, Runnable, String)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, and belongs to the thread group referred to by the group.

Thread(ThreadGroup, Runnable, String, long)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, belongs to the thread group referred to by the group, and has the specified stack size.

Thread(ThreadGroup, Runnable, String, long, boolean)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name belongs to the thread group referred to by group, has the specified stackSize, and inherits initial values for inheritable thread-local variables if inheritThreadLocals is true.

Thread.activeCount()This method returns an estimate of the number of active threads in the current thread's thread group and its subgroups. 

checkAccess()This method determines if the currently running thread has permission to modify this thread.

Constants Thread Class: Some of the constants of the Thread class, are Thread.MIN_PRIORITY, Thread.NORM_PRIORITY and Thread.MAX_PRIORITY.

Thread.currentThread()This method returns a reference to the currently executing thread object.

Thread.dumpStack()This method prints a stack trace of the current thread to the standard error stream.

Thread.enumerate(Thread[])This method copies into the specified array every active thread in the current thread's thread group and its subgroups.

Thread.getAllStackTraces()This method returns a map of stack traces for all live threads. The map keys are threads and each map value is an array of StackTraceElement that represents the stack dump of the corresponding Thread.

getContextClassLoader()This method returns the context ClassLoader for this thread.

Thread.getDefaultUncaughtExceptionHandler()This method returns the default handler invoked when a thread abruptly terminates due to an uncaught exception. If the returned value is null, there is no default.

getId()This method returns the identifier of this Thread. 

getName()This method returns this thread's name.

getPriority()This method returns this thread's priority.

getStackTrace()This method returns an array of stack trace elements representing the stack dump of this thread.

getState()This method returns the state of this thread.

getThreadGroup()This method returns the thread group to which this thread belongs.

No comments:

Post a Comment