我如何使用Java多线程从多个文件中读取? [英] How I can use Java multi-threading to read from multiple files?

查看:225
本文介绍了我如何使用Java多线程从多个文件中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读并解析很多文件。由于有超过10000个文件需要解析,我想通过使用线程来加快这个过程。

I want to read and parse a lot of files. Since there are over 10000 files that are to be parsed, I want to make this process faster by making use of threads.

例如,如果我有5个线程,我希望让它们同时读取一定数量的文件,以便更快地读取和解析过程。这可能吗?通过将其分成线程,我可以获得任何显着的加速吗?如果是这样,我该怎么做?

For example, if I had 5 threads, I want to have them all read a certain number of files concurrently so that the process of reading and parsing is faster. Is this possible? Would I gain any significant speed-up by splitting this up into threads? If so, how can I do this?

P.S。我并不反对使用外部库。

P.S. I am not against using external libraries.

我正在使用jdk 1.6

I am working with jdk 1.6

推荐答案

如果要读取的文件很多,最好的方法是每个文件只读一个线程。在大多数情况下,使用多线程处理许多任务的最佳方法是使用使用线程池的ExecutorService。为要读取的每个文件向服务提交任务。使线程池足够大以保持I / O系统繁忙(这可能是瓶颈)并且您将最大化性能。

If you have many files to read, the better approach is to have no more than one thread read each file. And the best way of handling many tasks with multiple threads , for most cases, is to use an ExecutorService that uses a thread pool. Submit a task to the service for each file to be read. Make the thread pool large enough to keep the I/O system busy (which is likely to be the bottleneck) and you will maximize performance.

这篇关于我如何使用Java多线程从多个文件中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆