- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么在某些 JVM/OS 组合上 java.nio.FileChannel transferTo() 和 transferFrom() 比逐字节传输(基于流或使用 ByteBuffer)更快???
这些方法是否使用直接内存访问 (DMA) 而不是为每个字节传输发出中断请求 (IRQ)?
最佳答案
Do these methods use direct memory access (DMA) as opposed to issuing interrupt requests (IRQ) for each byte transfer?
具体以实际实现为准,不要求使用任何特定机制。 transferTo
的文档中暗示了这一点(重点是我的):
This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.
“可能”、“很多”...所以无法保证。
假设使用该方法可能更有效是有道理的,即使只是略微如此,因为您允许 JVM 快捷方式通过 native 代码(如果使用受支持的 channel 类型)。他们描述的“简单循环”的工作原理如下所示:
ByteBuffer buf = ByteBuffer.allocateDirect(BUF_SIZE);
while ( /* read more condition */ ) {
source.read(buf);
buf.flip();
target.write(buf);
buf.compact();
}
请注意,即使此代码段使用直接缓冲区,您仍然需要回到 Java 中进行缓冲区管理(读取、翻转、写入、压缩)。优化编译器可能能够省略其中的一些,但它可能不会。
但是,使用transferTo
/transferFrom
将由 JVM 决定如何传输字节。如果平台对这种传输有原生支持,它可以在不创建中间缓冲区的情况下执行此操作。如果没有这样的支持,the JVM can still implement a loop such as above .
示例:假设 SocketChannel 被告知通过 transferFrom
直接从 FileChannel 读取数据。最近读取了 FileChannel,其内容在操作系统文件缓存中。 SocketChannel 可以直接指向操作系统文件缓存并从那里开始传输,而不是读取字节并将其复制到缓冲区中。至少消除了一轮复制。
现在进一步假设套接字 (A) 实际上连接到某个其他本地进程,例如使用一种称为 SocketChannel B 的管道。当 B 开始读取它从 A 获得的内容时,它实际上可能直接从操作系统文件缓存。如果 B 只是使用 transferTo
到另一个 channel ......你明白了。
关于jvm - 为什么 java.nio.FileChannel transferTo() 和 transferFrom() 更快???它使用DMA吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16451642/
我花了相当多的时间尝试优化文件哈希算法,以尽可能地提高性能。 查看我之前的 SO 主题: Get File Hash Performance/Optimization FileChannel Byte
我不太明白它们之间有什么不同,所以我对这两个包有一些疑问。 在 Google 上浏览了一下之后,似乎 Oracle 决定使用更新和增强的 NIO.2 包来更新 NIO 包,作为 JDK7 版本的一部分
在 Java 1.4 之前,通过在不同的输入流/输出流之间移动字节来处理文件是常见的做法。 自 Java 1.4 起,其中 NIO已添加,建议使用 Channels 执行相同操作。 与 NIO2在 J
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我需要重写一些应该在 Java 6 VM 上运行的 Java 7 文件 IO 代码。 该实现使用了方便的 Java 7 功能,例如自动关闭、Paths 和 Files。 更具体地说,我需要处理像 /t
当我查看java中Scanner的源代码时,我发现一些让我困惑的事情 import java.nio.file.Path; import java.nio.*; 它们之间有什么区别,为什么它们不直接导
我的 Java 代码中几乎所有文件 I/O 操作都使用 java.nio.*。然而,在今天调试一段代码时,我注意到调试器 (Intellij IDEA 14) 显示了以下关于 java.nio.fil
奇怪的是,我无法在 Google 中找到 NIO.2 异步 IO 性能与通过 java.nio.channels.Selector 使用 NIO 的多路复用 IO 的明确答案。 所以,我的问题是:NI
我是初级 Java 程序员。 今天,我练习了如何在 java 中复制文件并尝试按照本教程进行操作 http://www.journaldev.com/861/4-ways-to-copy-file-i
我有一个指向绝对路径的 java.nio.Path: /home/user/project/resources/configuration.xml 我有第二个 java.nio.Path 指向项目的根
我开始使用java.nio.*,现在我想知道:为什么java.nio.Paths.get(..)不使用java.nio.Path 对象作为输入? 现在我总是做这样的事情: final Path bas
我是新手,正在学习 Java。我尝试在 Netbeans 7 中运行以下应用程序。 import java.io.*; import java.nio.file.*; import java.nio.
我的 Java 程序(见下文)有时会在 java.nio.File.move() 方法执行中因 java.nio.file.AccessDeniedException 崩溃。 我不明白为什么会抛出这个
所以我在这里阅读我最喜欢的软件模式书籍之一(面向模式的软件架构 - 并发和网络对象的模式),特别是关于 Proactor/Reactor 异步 IO 模式的部分。我可以看到通过使用可选 channel
我有一个方法如下,它已经正常运行了很长时间: private String loadFromFile(){ RandomAccessFile inFile = null; FileCh
我在 IntellijIDEA Community Edition 2017.3 版本中收到以下错误。该项目使用java版本“1.8.0-ea”。请给我一些解决问题的想法 Error:Internal
一 点睛 在 scatter-and-gather 场景下,可以将数据写入多个 Buffer 中。在 NIO 中,也能够同时操作多个缓冲区。在很多 Channel 实现类中,都提供了多个重载的 rea
I/O简介 在 Java 编程中,直到最近一直使用 流 的方式完成 I/O。所有 I/O 都被视为单个的字节的移动,通过一个称为 Stream 的对象一次移动一个字节。流 I/O 用于与外部世界接
一 点睛 给某一个文件加锁,防止并发访问时引起的数据不安全。 在 JUC 中,可以使用 synchronized、Lock 给共享的资源加锁,或者使用 volatile、CAS 算法等防止并发冲突。在
一 点睛 给某一个文件加锁,防止并发访问时引起的数据不安全。 在 JUC 中,可以使用 synchronized、Lock 给共享的资源加锁,或者使用 volatile、CAS 算法等防止并发冲突。在
我是一名优秀的程序员,十分优秀!