gpt4 book ai didi

java - 为什么 Java 8 Stream 类可以自动关闭?

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:17 31 4
gpt4 key购买 nike

在 Java 8 中,Stream 类实现了 AutoCloseable。这意味着应该显式关闭流实例。

我理解为什么文件处理程序和数据库连接是可关闭的。但为什么要流?

最佳答案

我认为当前documentation/javadoc of Stream很清楚:

Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource in a try-with-resources statement.)

关于java - 为什么 Java 8 Stream 类可以自动关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22125169/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com