Java是否支持RAII /确定性破坏? [英] Does Java support RAII/deterministic destruction?

查看:129
本文介绍了Java是否支持RAII /确定性破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我使用Java以来​​已经至少5年了,当时,只要你想分配一个需要清理的对象(例如套接字,数据库句柄),你就必须记得添加一个 finally 阻止并在那里调用清理方法。

It's been at least 5 years since I worked with Java, and back then, any time you wanted to allocate an object that needed cleaning up (e.g. sockets, DB handles), you had to remember to add a finally block and call the cleanup method in there.

相比之下,在C ++(或其他语言中,对象生存期是确定性的,例如Perl),类实现者将定义一个析构函数,只要该类的对象超出范围,该函数就会执行清理。这种方法的优点是对象的用户不会忘记清理它 - 即使抛出异常,也会自动调用析构函数。 RAII的名称相当糟糕 - 资源获取是初始化。

By contrast, in C++ (or other languages where object lifetimes are deterministic, e.g. Perl), the class implementor would define a destructor function that performs the cleanup whenever an object of that class goes out of scope. The advantage of this approach is that the user of the object can't forget to clean it up -- the destructor gets called automatically, even if an exception is thrown. This approach goes by the fairly awful name of RAII -- "Resource Acquisition Is Initialisation".

根据我的经验,以RAII方式做事让我省去了在不必担心资源解除分配是否以及何时发生的情况下,需要大量精神开销。我们正在考虑将Java用于一个中型项目,我想知道自从我上次查看它以来,语言中添加的许多新功能是否存在某种确定性破坏。 (我希望我的抱怨是Java没有RAII被指责在这个帖子上,但到目前为止我还没能通过谷歌搜索找到任何细节。)

It's been my experience that doing things "the RAII way" has saved me a lot of mental overhead in terms of not having to worry about whether and when resource deallocations occur. We are considering using Java for a medium-sized project, and I'm wondering if some sort of deterministic destruction is among the many new features added to the language since I last looked at it. (I'm hopeful as my complaint that "Java has no RAII" was rebuked on this thread, but so far I haven't been able to find any details by googling.)

所以如果有人可以指点我介绍一下关于如何在Java中解决这个问题的材料很棒!

So if someone could point me to some introductory material on how to go about this in Java that would be great!

推荐答案

编辑:以下答案写于2009年初,当Java 7仍然处于不稳定状态时。

The answer below was written in early 2009, when Java 7 was very much still in flux.

虽然Java仍无法提供有关最终确定时间的保证,但 获得了类似的功能C#的使用语句:试用资源声明

While Java still doesn't provide guarantees around finalization timing, it did gain a feature like C#'s using statement: the try-with-resources statement.

不,Java在这方面根本没有改变。你仍然需要使用try / finally。

No, Java hasn't changed at all in that respect. You still need to use try/finally.

有人讨论过向Java添加等效的C#的using语句(这是对try / finally的语法糖),但我不认为这将成为Java 7的一部分。 (大多数语言改进似乎都已被删除。)

There's been discussion of adding the equivalent of C#'s "using" statement (which is syntactic sugar over try/finally) to Java, but I don't think that's going to be part of Java 7 any more. (Most of the language improvements seem to have been dropped.)

值得理解的是,为什么在Java和.NET中没有实现确定性破坏的原因顺便说一下,参考计数垃圾收集器的形式 - a)影响性能,b)通过循环引用失败。 Brian Harry写了一篇关于这个的详细的电子邮件 - 这是关于.NET的,而且相当于旧的,但值得仔细阅读。

It's worth understanding that there are reasons why deterministic destruction hasn't been implemented in Java and .NET in the form of a reference-counted garbage collector, by the way - that a) impacts performance and b) fails with circular references. Brian Harry wrote a detailed email about this - it's about .NET and it's rather old, but it's well worth a close read.

这篇关于Java是否支持RAII /确定性破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆