Java线程类void setContextClassLoader(ClassLoader loader)方法,带示例

线程类void setContextClassLoader(ClassLoader loader) (Thread Class void setContextClassLoader(ClassLoader loader))

  • This method is available in package java.lang.Thread.setContextClassLoader(ClassLoader loader).

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

  • This method is used to sets the context ClassLoader for this(Current)Thread.

    此方法用于为此(当前)线程设置上下文ClassLoader。

  • 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.

    可以在创建线程时设置上下文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 void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

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

    如果此线程无法设置上下文ClassLoader,则此方法引发异常(SecurityException)。

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.

我们仅在Thread方法中传递一个对象作为参数,并且该参数是该线程的上下文ClassLoader。

Return value:

返回值:

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

此方法的返回类型为void ,它不返回任何内容。

Java程序演示setContextClassLoader()方法的示例 (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 = [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-void-setcontextclassloader-classloader-loader-method-with-example.aspx

Java POI提供了一种多线程导出Excel的方式,可以提高导出效率。 以下是一个简单的示例代码: ```java public class ExcelExportThread extends Thread { private Workbook workbook; private OutputStream outputStream; public ExcelExportThread(Workbook workbook, OutputStream outputStream) { this.workbook = workbook; this.outputStream = outputStream; } @Override public void run() { try { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public class ExcelExporter { public static void export(List<List<Object>> dataList, int sheetSize, OutputStream outputStream) { Workbook workbook = new XSSFWorkbook(); int dataSize = dataList.size(); int sheetNum = dataSize % sheetSize == 0 ? dataSize / sheetSize : dataSize / sheetSize + 1; CountDownLatch latch = new CountDownLatch(sheetNum); for (int i = 0; i < sheetNum; i++) { int start = i * sheetSize; int end = Math.min(start + sheetSize, dataSize); List<List<Object>> subList = dataList.subList(start, end); ExcelExportThread thread = new ExcelExportThread(createSheet(workbook, subList), outputStream); thread.start(); thread.setUncaughtExceptionHandler((t, e) -> { // 异常处理 }); thread.setContextClassLoader(null); thread.setName("ExcelExportThread-" + i); thread.setPriority(Thread.NORM_PRIORITY); thread.setDaemon(false); thread.start(); } try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } private static Sheet createSheet(Workbook workbook, List<List<Object>> dataList) { Sheet sheet = workbook.createSheet(); int rowIndex = 0; for (List<Object> rowData : dataList) { Row row = sheet.createRow(rowIndex++); int colIndex = 0; for (Object cellData : rowData) { Cell cell = row.createCell(colIndex++); cell.setCellValue(cellData.toString()); } } return sheet; } } ``` 该示例中,ExcelExporter提供了一个静态方法export,用于导出Excel。该方法接收三个参数:数据列表、每个Sheet的最大行数、输出流。 export方法首先创建一个新的Workbook实例,然后根据每个Sheet的最大行数将数据列表拆分为多个子列表,并创建ExcelExportThread实例进行导出。每个ExcelExportThread实例会创建一个Sheet,并将数据写入Sheet中。多个线程导出,提高了导出效率。 在ExcelExportThread的run方法中,使用Workbook的write方法将数据写入输出流,导出Excel文件。导出完成后,关闭输出流。 在示例中,使用了CountDownLatch来等待所有线程导出完成。如果线程中出现异常,可以在ExcelExportThreadsetUncaughtExceptionHandler方法中进行处理。其他线程属性设置可以根据实际情况进行调整。 需要注意的是,由于多线程导出Excel可能会占用大量的内存和CPU资源,可能会导致系统负载过高,因此需要根据实际情况进行调整。同多线程导出Excel也可能会导致导出结果的顺序发生变化,需要注意处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值