Java线程类ClassLoader getContextClassLoader()方法(带示例)

线程类ClassLoader getContextClassLoader() (Thread Class ClassLoader getContextClassLoader())

  • This method is available in package java.lang.Thread.getContextClassLoader().

    软件包java.lang.Thread.getContextClassLoader()中提供了此方法。

  • This method is used to return a context ClassLoader for this (Current) Thread.

    此方法用于返回此(当前)线程的上下文ClassLoader。

  • This method is not static so this method is accessible with Thread class object it is not accessible with the class name.

    此方法不是静态的,因此该方法可通过Thread类对象访问,而无法通过类名称访问。

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

    该方法的返回类型为ClassLoader,因此它返回上下文ClassLoader。

  • This method raises an exception (SecurityException) if this thread did not get the context ClassLoader.

    如果此线程未获取上下文ClassLoader,则此方法引发异常(SecurityException)。

Syntax:

句法:

    ClassLoader getContextClassLoader(){
    }

Parameter(s):

参数:

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

我们不会在Thread方法中将任何对象作为参数传递。

Return value:

返回值:

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

该方法的返回类型为ClassLoader ,它返回上下文ClassLoader。

Java程序演示getContextClassLoader()方法的示例 (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


翻译自: https://www.includehelp.com/java/thread-class-classloader-getcontextclassloader-method-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值