log4j是否使用NIO将数据写入文件?

发布于 2024-09-07 14:15:27 字数 146 浏览 5 评论 0原文

它似乎已经相当快了,我只是想知道是否有人知道它是否使用 NIO。我尝试搜索 NIO 的整个源代码(嗯,这是一种搜索方式:)哈哈);但没有击中任何东西。另外,如果它不使用NIO;您认为是否值得修改 log4j 以使用 NIO 来使其更快?任何指示建议和一些资源的链接都会很有帮助。

It seems to be pretty fast already and i was just wondering if anyone knows if its using NIO. I tried searching the whole source code for NIO (well, its kind of way to search :) lol); but did not hit anything. Also, If its not using NIO; do you think its worthwhile to modify log4j to use NIO also to make it more faster? Any pointers advises and links to some resources would be lot helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

隐诗 2024-09-14 14:15:27

另外,如果它不使用NIO;你
认为值得修改
log4j 使用 NIO 也让它变得更多
更快?

不可以,除非日志记录是应用程序活动的重要组成部分,在这种情况下通常会出现问题。

您似乎认为蔚来“更快”,但总体而言并非如此。只需尝试创建两个文件,一个使用标准 IO,另一个使用 NIO,向其中写入一堆数据并关闭它们。您会发现性能几乎没有什么不同。 NIO只会在某些用例中表现更好;最常见的是许多连接的情况。

Also, If its not using NIO; do you
think its worthwhile to modify of
log4j to use NIO also to make it more
faster?

No, unless logging is a significant part of your application's activities, in which case there is usually something wrong.

You seem to be under the impression that NIO 'is faster', but this is not true in general. Just try creating two files, one with standard IO and one with NIO, writing a bunch of data to them and closing them. You'll see the performance hardly differs. NIO will only perform better in certain use cases; most usually the case of many connections.

暮凉 2024-09-14 14:15:27

查看 FileAppender 源。几乎只是标准的java.io

Check out the FileAppender source. Pretty much just standard java.io.

凑诗 2024-09-14 14:15:27

在这种情况下,我看不出 FileChannel 比 FileOutputStream 更快的任何原因。

也许通过使用MappedByteBuffer?但在追加模式下,行为取决于操作系统。

最终,性能取决于您的硬盘驱动器,您的代码无关紧要。

I don't see any reason why FileChannel could be any faster than FileOutputStream in this case.

maybe by using MappedByteBuffer? but in append mode, the behavior is OS dependent.

ultimately, the performance depends on your hard drive, your code matters very little.

遗心遗梦遗幸福 2024-09-14 14:15:27

详细阐述 Confusion 的答案,File NIO 也会阻塞。这就是为什么它在某些场景中并不比传统文件IO更快。引用 O'Reilly 的 Java NIO 书

文件通道始终处于阻塞状态,无法放入
非阻塞模式。现代操作系统具有复杂的缓存
和预取算法通常使本地磁盘 I/O 非常低
延迟。网络文件系统通常具有较高的延迟,但通常
受益于相同的优化。非阻塞范例
面向流的 I/O 对于面向文件的 I/O 没有多大意义
由于文件 I/O 的本质不同,因此操作。
对于文件 I/O,真正的赢家是异步 I/O,它让
进程向操作系统请求一个或多个 I/O 操作
但不等待他们完成。该流程的通知时间为
稍后请求的 I/O 完成。异步 I/O 是
许多操作系统不具备的高级功能。这是
正在考虑作为未来 NIO 的增强功能。

编辑:话虽如此,如果将 File NIO 与 MappedByteBuffer 一起使用,您可以获得更好的读/写效率。请注意,在 Log4j 2 中使用 MappedByteBuffer 正在考虑中。

Elaborating on Confusion's answer, File NIO blocks as well. That's why it is not faster than traditional File IO in some scenarios. Quoting O'Reilly's Java NIO book:

File channels are always blocking and cannot be placed into
nonblocking mode. Modern operating systems have sophisticated caching
and prefetch algorithms that usually give local disk I/O very low
latency. Network filesystems generally have higher latencies but often
benefit from the same optimizations. The nonblocking paradigm of
stream-oriented I/O doesn't make as much sense for file-oriented
operations because of the fundamentally different nature of file I/O.
For file I/O, the true winner is asynchronous I/O, which lets a
process request one or more I/O operations from the operating system
but does not wait for them to complete. The process is notified at a
later time that the requested I/O has completed. Asynchronous I/O is
an advanced capability not available on many operating systems. It is
under consideration as a future NIO enhancement.

Edit: With that said, you can get better read/write efficiency if you use File NIO with a MappedByteBuffer. Note that using MappedByteBuffer in Log4j 2 is under consideration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文