多线程拆分大文件,并计算耗时

public class Test {

    public static void main(String[] args) {
        String srcPath = "c:\\test\\main.txt";
        splitFile(srcPath);
    }

    public static void splitFile(String srcPath) {
        BufferedReader bufferedReader = null;
        try {
            File file = new File(srcPath);
            bufferedReader = new BufferedReader(new FileReader(file));
            String line = null;
            int lineNum = 0;
            String symbolContent = null;
            while ((line = bufferedReader.readLine()) != null) {
                lineNum++;
                if (lineNum == 1) {
                    symbolContent = line;
                }
            }
            System.out.println("第一行数据:" + symbolContent);
            int total = Integer.valueOf(symbolContent.substring(11));
            System.out.println("记录总数:" + total);
            int threadNum = 10;
            //计算每个线程处理记录数
            int singleProcess = 0;
            if (total % threadNum == 0) {
                singleProcess = total / threadNum;
            } else {
                singleProcess = (total / threadNum) + (total % threadNum);
            }
            System.out.println("每个线程处理记录数:" + singleProcess);
            CountDownLatch latch = new CountDownLatch(threadNum);
            long start = System.currentTimeMillis();
            for (int i = 0; i < threadNum; i++) {
                int startIndex = i * singleProcess;
                MultiThreadReadFile multiThreadReadFile = new MultiThreadReadFile(file, startIndex, singleProcess, latch);
                Thread thread = new Thread(multiThreadReadFile);
                thread.start();
            }
            latch.await();
            long end = System.currentTimeMillis();
            System.out.println("拆分文件耗时:" + (end - start) + "毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public class MultiThreadReadFile implements Runnable {

    private File file;
    private int startIndex;
    private int size;
    private CountDownLatch latch;

    public MultiThreadReadFile(File file, int startIndex, int size, CountDownLatch latch) {
        this.file = file;
        this.startIndex = startIndex;
        this.size = size;
        this.latch = latch;
    }

    @Override
    public void run() {
        readFile(file, startIndex, size);
    }

    public void readFile(File file, int startIndex, int size) {
        String outFilePath = "c:\\test\\dir.txt";
        File outFile = new File(outFilePath);
        LineNumberReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new LineNumberReader(new FileReader(file));
            writer = new BufferedWriter(new FileWriter(outFile));
            int endIndex = startIndex + size;
            String line = null;
            int currentNum = 0;
            while ((line = reader.readLine()) != null && (currentNum = reader.getLineNumber()) < (endIndex + 2)) {
                if (currentNum != 1 && currentNum >= startIndex + 2) {
                    writer.write(line + "\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            latch.countDown();
        }
    }
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多线程是指在一个程序中同时执行多个线程的编程模型。每个线程都是独立的执行路径,可以独立地运行和完成任务。 多线程的优势包括: 1. 提高程序的响应性:使用多线程可以将耗时的操作放到后台线程中执行,使得前台线程能够快速响应用户的操作。例如,在图形界面应用程序中,可以将耗时的数据加载或计算操作放到后台线程中,以保持界面的流畅性。 2. 提高系统的资源利用率:多线程可以充分利用多核处理器的优势,同时执行多个任务,提高系统的资源利用率。这对于需要处理大量并发请求或并行计算任务的应用程序非常有益。 3. 简化程序设计:使用多线程可以将复杂的任务拆分成多个线程,每个线程负责一部分工作,从而简化程序的设计和实现。例如,在网络服务器中,可以使用多线程来处理并发的客户请求,每个线程负责一个客户连接。 4. 共享数据和通信方便:多线程之间可以共享数据,这样不同线程之间的通信更加方便。通过合理地使用锁、信号量、管道等同步机制,可以实现线程间的数据共享和通信。 5. 并发编程的概念和技术:多线程编程是并发编程的基础,掌握多线程编程可以为学习和理解更复杂的并发编程概念和技术(如线程池、并发集合、消息传递等)打下基础。 需要注意的是,多线程编程也带来了一些挑战和注意事项,如线程安全问题、死锁、竞态条件等。因此,在设计和实现多线程程序时,需要仔细考虑并合理处理这些问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值