- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我曾尝试为 Java 实现自动资源管理(类似于 C# 的 using
)。以下是我想出的代码:
import java.lang.reflect.*;
import java.io.*;
interface ResourceUser<T> {
void use(T resource);
}
class LoanPattern {
public static <T> void using(T resource, ResourceUser<T> user) {
Method closeMethod = null;
try {
closeMethod = resource.getClass().getMethod("close");
user.use(resource);
}
catch(Exception x) {
x.printStackTrace();
}
finally {
try {
closeMethod.invoke(resource);
}
catch(Exception x) {
x.printStackTrace();
}
}
}
public static void main(String[] args) {
using(new PrintWriter(System.out,true), new ResourceUser<PrintWriter>() {
public void use(PrintWriter writer) {
writer.println("Hello");
}
});
}
}
请分析上面的代码,让我知道任何可能的缺陷,并建议我如何改进它。谢谢。
(抱歉我的英语不好。我的母语不是英语。)
最佳答案
我会像这样修改您的using
方法:
public static <T> void using(T resource, ResourceUser<T> user) {
try {
user.use(resource);
} finally {
try {
Method closeMethod = resource.getClass().getMethod("close");
closeMethod.invoke(resource);
} catch (NoSuchMethodException e) {
// not closable
} catch (SecurityException e) {
// not closable
}
}
}
另外,您需要为资源不可关闭 的情况定义您想要的行为(当您捕捉到上述异常时)。您可以抛出一个特定的异常,如 UnclosableResourceException
或完全忽略这种情况。您甚至可以使用这 2 种行为(using
和 tryUsing
)实现 2 种方法。
关于c# - Java中的贷款模式/自动资源管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2538559/
我有一个 12 节点集群,我运行的是 yarn 架构。似乎我的节点大部分时间都很忙,而且很多时候作业都失败了。如何随时查看资源的使用情况?还有什么方法可以为用户设置有限的资源,例如:如果用户提交作业,
我有一个关于 Xtend 的非常具体的问题。 在我读到的有关 xText/xTend 的每个示例中,我都会看到类似这样的内容: override void doGenerate(Resource re
代码参考 代码文件参考下述详解的类图,工程参考第零章工程说明 。 概述 在游戏项目中有很多资产如:预制体,图片,音频,Lua脚本,Shader等等。他们随打包放在用户的硬盘里。在游戏的运
我有一个包含10个节点的Hadoop集群。在10个节点中,有3个部署了HBase。有两个共享集群的应用程序。 应用程序1从hadoop HDF写入和读取数据。应用程序2将数据存储到HBase中。有没有
例子: var image = UIImage.FromFile("/path/to/image.png"); var imageView = new UIImageView(); imageView
我需要存储 ThirdPartyElm 类型的元素序列,我使用的是 std::vector(或 std::array 如果我需要一个固定大小的序列)。 我想知道我应该如何初始化序列。第一个版本创建一个
我有一个具有UITabBarController的MonoTouch应用程序,每个选项卡都是一个UINavigationController。其中一些包装了一个UIViewController,后者添
所以我一直在研究使用资源管理 API 对 azure 进行只读 api 访问。现在我主要关注虚拟机。我一直在使用这个预发布包和 TokenCredentials: https://www.nuget.
我想获取 Azure 中包含给定标签的资源列表。基本请求形式如下:https://management.azure.com/subscriptions/{subscriptionId}/resourc
我一直在尝试访问 Azure 公开的 REST API,例如“https://management.azure.com/subscriptions?api-version=2016-06-01”,但无
如何使用 Azure 资源管理 API 获取资源组的资源列表 我已经安装了 Microsoft.Azure.Management.ResourceManager.Fluent Nuget 包下面的脚本
如何使用 Azure 资源管理 API 获取资源组的资源列表 我已经安装了 Microsoft.Azure.Management.ResourceManager.Fluent Nuget 包下面的脚本
我正在尝试在 Jersey 中编写 RESTful Web 应用程序时管理竞争资源(如:数据库 session )。通常我会写这样的代码: Session session = getSession()
我是这个论坛的新手。 我正在查看有关 Azure 资源运行状况 Rest Api 的 Microsoft 文档,链接如下: https://azure.microsoft.com/en-us/blog
我想使用这些 API 获取特定 Azure 帐户或订阅的运行状况和其他资源详细信息 https://learn.microsoft.com/en-us/rest/api/resourcehealth/
我正在尝试在 Azure 资源管理的上下文中找到应用程序权限的安全最佳实践。 目前,management.azure.com 仅列出了一项权限,它是management.azure.com/user_
我想使用这些 API 获取特定 Azure 帐户或订阅的运行状况和其他资源详细信息 https://learn.microsoft.com/en-us/rest/api/resourcehealth/
我正在尝试在 Azure 资源管理的上下文中找到应用程序权限的安全最佳实践。 目前,management.azure.com 仅列出了一项权限,它是management.azure.com/user_
我正在寻找一种以编程方式列出组织内的 Google Cloud 项目的方法。我正在尝试使用服务帐户导出的 json 凭据以这种方式实现此目的: // More info on the endp
我正在编写一些用于自动部署Azure网站的代码(包括在Azure中创建网站)。我正在使用 Nuget 中提供的 Azure 管理库和 Azure 资源管理库。大部分内容都已到位,但是我无法找到通过我见
我是一名优秀的程序员,十分优秀!