使用3个线程读取单个文件
我想要三个线程读取单个文件,例如文件的大小是 900 kb 我希望第一个线程读取文件 1kb 到 300,并且以与其他线程相同的方式执行操作(2 个线程从 301 kb 读取到 600 kb 并且 3线程读取 601kb 到 900kb)这种方法是否可以使读取速度更快 输出必须显示在控制台上 可能输出会混合,这对我来说并不重要 主要问题是如何与单线程相比,阅读速度更快,请给我建议或编码(如果有人有)
i want three thread reading the single file for example the size of the file is 900 kb i want the first thread read the file 1kb to 300 and in the same fashion the other thread do (2 thread read from 301 kb to 600 kb AND 3 thread read 601kb to 900kb) does this approach make reading faster output has to be shown on the console may be the output get mixed it does not matter for me The main matter is that how to read the faster as comparison to single thread plz plz give me a suggestion or coding if somebody have plz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信,如果您的目标是性能,那么您不应该费心在多个线程中读取单个兆字节文件。大部分时间可能花在实际的 IO 操作上,即从磁盘读取(回想一下,磁盘操作比内存操作慢数百万倍)。当然,在某些情况下,它可能会更快(例如,在Linux系统上,文件数据可能已经被缓存,如果它之前已经被读取或写入过一段时间)。
但是,当读取(相当小的,即兆字节大小)文件时,大部分时间都花在系统上,并且您的编码不会影响这一点。
在当今的机器上读取兆字节文件应该很快。您可能会使用一些肮脏的系统技巧来改进它(例如 Linux readahead 系统调用) ,如果绝对必要的话。
事实上,你的问题让我感到惊讶。今天读取一兆字节很快!
I believe that if your goal is performance, you should not bother reading a single megabyte file in several threads. Most of the time is probably spent in doing the actual IO operation, that is reading from the disk (recall that disk operations are millions times slower than memory operations). Of course, on some occasions, it could be faster (e.g. on Linux system, the file data could have been cached, it it has been read or written some time before).
But when reading (rather small, i.e. megabyte sized) files, most of the time is spent in the system, and your coding won't impact that.
And reading a megabyte file should go fast on today's machines. You might use some dirty system tricks to improve it (e.g. the Linux readahead system call), if absolutely necessary.
Actually, your question surprises me. Reading one megabyte is quick today!