创建多线程同时处理一个文件的导入

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FileImportExample {
    public static void main(String[] args) {
        String filename = "path/to/your/file.txt";
        int threadCount = 4; // 指定线程数量

        List<String> lines = readFile(filename);
        int totalLines = lines.size();

        // 计算每个线程处理的行数
        int linesPerThread = totalLines / threadCount;

        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

        // 启动多个线程处理文件导入
        for (int i = 0; i < threadCount; i++) {
            int startLine = i * linesPerThread;
            int endLine = (i == threadCount - 1) ? totalLines - 1 : (startLine + linesPerThread - 1);

            List<String> subList = lines.subList(startLine, endLine + 1);
            
            executorService.execute(() -> {
                processLines(subList);
            });
        }

        // 关闭线程池
        executorService.shutdown();
    }

    private static List<String> readFile(String filename) {
        List<String> lines = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

    private static void processLines(List<String> lines) {
        // 处理每行数据的逻辑
        for (String line : lines) {
            // 处理逻辑
            // ...
        }
    }
}

标签: none

相关阅读

  • 测试信息
  • 开发商:阿里巴巴
  • 版本号:1.0
  • 配色:(企业家)” 推选活动结果
  • 测试信息
  • 测试信息
  • 测试信息