问题标签 [try-with-resources]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - 错误的资源泄漏警告?
我在此代码段中收到有关“s”和“p”的资源泄漏警告。这个警告有效吗?
java - IntelliJ IDE 在将 Try-Catch 与资源一起使用时出错
我正在尝试使用 JDK 7 的“try-catch with resources”语句;IntelliJ 突出显示我的资源行,说
此语言级别不支持使用资源尝试。
当我尝试编译时,我得到:
java:-source 1.6 不支持 try-with-resources(使用 -source 7 或更高版本来启用 try-with-resources)
我检查了我当前的项目是否启用了 try-with-resources,并且我的项目正在使用 JDK 7(库:C:\Program Files\Java\jdk1.7.0_11)。有任何想法吗?我不知道要改变什么选项(如果这甚至是问题的话)。
java - 使用 Java 1.7 处理套接字的最新技术是什么?
我正在寻找使用 Java 1.7 ( try-with-resources ) 的适当特性来实现客户端/服务器套接字通信的最佳模式。必须确保所有线程和资源都完全关闭和释放。应考虑有效的 Java / Clean Code项以及简单的可调试性(每行中只有一个语句)。
任何反馈或讨论将不胜感激。
第一种方法
优点:
- 更少的代码行
缺点:
- 大型 try-with-resources 块(Eclipse 格式化程序不支持)
Socke
使用 try-with-resources 所需的第二个t 实例- 由于
UnknownHostException
from的深层嵌套InetAddress#getByName(null)
第二种方法
优点:
- 干净且可读的 try-with-resources 块
- 支持 Eclipse 格式化程序
缺点:
- 更多代码行
- 需要自己的
Connection
班级 - 可能会忘记关闭类
AutoCloseable
中的实例Connection
(容易出错)
您对利弊有反馈或补充吗?还有更多的弱点吗?
第三种方法
这是下面讨论的结果:
优点:
- 更少的代码行
- 拒绝复杂且最易读的 try-with-resources 块
- 没有异常
缺点:
- Eclipse 格式化程序不支持
- 一些违反规则“每行只有一个语句”(简单可调试性)
java - ServerSocket 不适用于 try-with-resources?
所以我们在课堂上玩弄 ServerSockets,制作一个非常简单的 HTTP 服务器,它接受一个请求,不做任何事情,并以 200 OK 响应,然后是一些 HTML 内容。
这两天我一直在想办法解决这个问题,我一直没能掌握,我的老师也没有。由于某些奇怪的原因,我开始认为关闭服务器是一个问题。我已经解决了这个问题,但只想知道为什么我首先会发生这种情况。
以下是三个片段:
HttpServer.class:
有效的 Main.class 解决方案:
不起作用的 Main.class 解决方案:
I could imagine it has something to do with not closing the client socket after each loop iteration. But even so, it should at least go through once, before bugging up in that case. I really can't see what the problem is supposed to be.
No error messages, nothing...
java - 使用新的 try-with-resources 块在 SQLException 上回滚事务
我对 try-with-resources 有疑问,我只是想确定一下。如果我需要对异常做出反应,并且我仍然需要 catch 块中的资源,我可以使用它吗?给出的例子是这样的:
我担心在这种情况下我仍然注定要使用旧的 try-catch-finally,即使根据 oracle 文档 - “catch and finally blocks in a try-with-resources statement, any catch or finally block are run after the resources宣布已关闭。”
java - Is this file object left open?
As an exercise for school I wrote a method in Java that searches for a character in a file. Here is the code:
This does what I need it to but I have a question. Is the file object ever opened? And if it is, does it ever closed? I used try-with-resources on the Scanner object so I'm pretty sure I don't have to explicitly close that, but what about the file object?
java - Java 扫描器是否实现 Closeable?
我昨天问了这个问题。我想我得到了正确的答案,但其他答案之一给我留下了一个问题。如果我有这样的代码:
这是错的吗?据我了解,如果该资源实现Closeable或AutoCloseable ,则try-with-resources语句将关闭该资源。在我看来,我将此等同于使用with语句在 Python 中打开文件资源。但是@David Newcomb 的回答说 Scanner 不是Closeable。
我查看了Java源代码,发现了这一行:
这对我来说意味着我使用try-with-resources是安全的,并且文件资源将在 try 块结束时关闭,而无需显式调用file.close()。我是对的还是应该做些不同的事情?
java - java.util.concurrent.locks.Lock 的 AutoCloseable 包装器有什么风险吗?
随着try-with-resource
在 Java 7 中的引入,我惊讶地发现它Lock
并没有被改造为AutoCloseable
. 看起来很简单,所以我自己添加了如下:
这适用于一个AutoCloseableReentrantReadWiteLock
类,用法如下:
由于这似乎是自动关闭RAII的直接和规范使用,我认为一定有充分的理由不应该这样做。有人知道吗?
java - 为什么 try-with-resource 需要一个局部变量?
参考我的问题java.util.concurrent.locks.Lock 的 AutoCloseable 包装器中是否存在任何风险?,我想知道为什么 try-with-resource-statement 需要一个命名的局部变量。
我目前的使用情况如下:
该变量l
在 try 块内未使用,只会污染命名空间。据我所知,类似的 C#using
语句不需要局部命名变量。
是否有任何原因无法支持以下内容,匿名局部变量在 try 块结束时关闭?
java - 为什么 try-with-resources 不能与字段变量一起使用?
这是我关于 SO 的第一个问题,我很困惑还没有类似的问题!
所以问题是:
为什么 try-with-resources 不能与字段变量一起使用?
或者换句话说:为什么我总是需要一个局部变量呢?
这里有一些示例代码:
谁能解释一下为什么会有这个约定?