使用3个线程读取单个文件

发布于 2024-12-09 23:57:37 字数 208 浏览 0 评论 0原文

我想要三个线程读取单个文件,例如文件的大小是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-12-16 23:57:37

我相信,如果您的目标是性能,那么您不应该费心在多个线程中读取单个兆字节文件。大部分时间可能花在实际的 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!

_失温 2024-12-16 23:57:37
public static void main(String[] args){
//
String filePath = args[0];

//Create runnable objects



//Load the file
BufferedReader br = new BufferedReader(new FileReader(filePath));
//share this object among threads you want
MyFileReader mf1 = new MyFileReader(br);
MyFileReader mf2 = new MyFileReader(br);
MyFileReader mf3 = new MyFileReader(br);

new Thread(mf1).start();
new Thread(mf2).start();
new Thread(mf3).start();
//code to detect thread ends
//close br here

}


public class MyFileReader implements Runnable{

private BufferedReader br=null;

public MyFileReader(BufferedReader br){
this.br = br
}

public void run(){
String line=null;
while((line=br.readLine())!=null){
//do your thing here e.g.
System.out.println(line);
}   
}
public static void main(String[] args){
//
String filePath = args[0];

//Create runnable objects



//Load the file
BufferedReader br = new BufferedReader(new FileReader(filePath));
//share this object among threads you want
MyFileReader mf1 = new MyFileReader(br);
MyFileReader mf2 = new MyFileReader(br);
MyFileReader mf3 = new MyFileReader(br);

new Thread(mf1).start();
new Thread(mf2).start();
new Thread(mf3).start();
//code to detect thread ends
//close br here

}


public class MyFileReader implements Runnable{

private BufferedReader br=null;

public MyFileReader(BufferedReader br){
this.br = br
}

public void run(){
String line=null;
while((line=br.readLine())!=null){
//do your thing here e.g.
System.out.println(line);
}   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文