Java多线程读取大文件教程

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.CharsetUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.*;

public class Test {


    public static void main(String[] args) {
        File file = new File("test.txt");
        // 创建线程池,核心线程数=CPU核心数,有界队列防止OOM
        //根据阿里规约 建议开发时使用 ThreadPoolExecutor 定义线程池
        int coreThreads = Runtime.getRuntime().availableProcessors();
        ExecutorService executor = new ThreadPoolExecutor(
                coreThreads,
                coreThreads,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1000),
                ThreadUtil.newNamedThreadFactory("file-processor-", false),
                new ThreadPoolExecutor.CallerRunsPolicy() // 队列满时由主线程处理
        );

        try (BufferedReader reader = FileUtil.getReader(file, CharsetUtil.CHARSET_UTF_8)) {
            String line;
            while ((line = reader.readLine()) != null) {
                final String currentLine = line; // 捕获当前行
                executor.execute(() -> processLine(currentLine));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            executor.shutdown(); // 停止接收新任务
            try {
                // 等待所有任务完成,最多等待1小时
                if (!executor.awaitTermination(1, TimeUnit.HOURS)) {
                    System.err.println("线程池未能在指定时间内终止");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static void processLine(String line) {
        // 模拟业务处理,如数据解析、存储等
        System.out.println(Thread.currentThread().getName() + " 处理: " + line);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值