在java中执行IO操作的最佳线程数是多少? [英] What is the optimal number of threads for performing IO operations in java?

查看:240
本文介绍了在java中执行IO操作的最佳线程数是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Goetz的实践中的Java并发中,在第101页的脚注中,他写道:对于这样的计算问题,不进行I / O并且不访问共享数据,Ncpu或Ncpu + 1线程可以产生最佳吞吐量;更多线程没有帮助,实际上可能会降低性能......

In Goetz's "Java Concurrency in Practice", in a footnote on page 101, he writes "For computational problems like this that do not I/O and access no shared data, Ncpu or Ncpu+1 threads yield optimal throughput; more threads do not help, and may in fact degrade performance..."

我的问题是,当执行文件写入,文件读取,文件删除等I / O操作时等,是否有用于实现最大性能的线程数的指导原则?我知道这只是一个指导编号,因为磁盘速度和许多其他因素都会影响到这一点。

My question is, when performing I/O operations such as file writing, file reading, file deleting, etc, are there guidelines for the number of threads to use to achieve maximum performance? I understand this will be just a guide number, since disk speeds and a host of other factors play into this.

不过,我想知道:在4-cpu机器上,20个线程可以将1000个独立文件写入磁盘的速度比4个线程快吗?

Still, I'm wondering: can 20 threads write 1000 separate files to disk faster than 4 threads can on a 4-cpu machine?

推荐答案

实际上,I / O绑定的应用程序仍然可以从多线程中获益,因为并行读取或写入一些文件要快得多。特别是在整体吞吐量受网络延迟影响的情况下。但同样的情况是,一个线程可以处理它读取的最后一个东西,而另一个线程正在忙着读取,从而允许更高的CPU利用率。

In practice, I/O-bound applications can still benefit substantially from multithreading because it can be much faster to read or write a few files in parallel than sequentially. This is particularly the case where overall throughput is compromised by network latency. But it's also the case that one thread can be processing the last thing that it read while another thread is busy reading, allowing higher CPU utilization.

我们可以整天谈论理论,但正确的答案是使线程数可配置。我认为你会发现将它增加到1会提高你的速度,但也会有一个收益递减点。

We can talk theory all day, but the right answer is to make the number of threads configurable. I think you'll find that increasing it past 1 will boost your speed, but there will also come a point of diminishing returns.

这篇关于在java中执行IO操作的最佳线程数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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