Java 中可以使用 RAII 吗?

发布于 2024-08-18 06:55:30 字数 241 浏览 8 评论 0原文

http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

这种设计模式在Java中是否可行?如果是这样,怎么办?如果没有,为什么不呢?

谢谢!

http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

Is this design pattern possible in Java? If so, how? If not, why not?

Thanks!

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

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

发布评论

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

评论(2

旧时浪漫 2024-08-25 06:55:30

您链接到的同一维基百科页面有一个关于 Java 的部分,引用:

因此,未引用对象的“finalize”方法可能永远不会被调用,或者仅在该对象成为未引用对象很久之后才被调用。因此,程序员必须使用处置模式之类的方式手动关闭资源。

void java_example() {
  // open file (acquire resource)
  final LogFile logfile = new LogFile("logfile.txt");

  try {
      logfile.write("hello logfile!");

      // continue using logfile ...
      // throw exceptions or return without worrying about closing the log;
      // it is closed automatically when exiting this block
   } finally {
      // explicitly release the resource
      logfile.close();
   }
}

每次使用资源时,释放资源的负担就落在程序员身上。

我认为 Java 7 有一个提案,它将创建 Closeable 类,以及 try 块的一些语法糖,以使其更加简洁(但您仍然需要编写 trytry代码>块)。

The same Wikipedia page that you linked to has a section on Java, quote:

Hence the "finalize" method of an unreferenced object might be never called or called only long after the object became unreferenced. Resources must thus be closed manually by the programmer, using something like the dispose pattern.

void java_example() {
  // open file (acquire resource)
  final LogFile logfile = new LogFile("logfile.txt");

  try {
      logfile.write("hello logfile!");

      // continue using logfile ...
      // throw exceptions or return without worrying about closing the log;
      // it is closed automatically when exiting this block
   } finally {
      // explicitly release the resource
      logfile.close();
   }
}

The burden of releasing resources falls on the programmer each time a resource is used.

I think there is a proposal for Java 7, which would create Closeable classes, and some syntactic sugar for the try blocks to make this more concise (but you still have to write that try block).

z祗昰~ 2024-08-25 06:55:30

如果在同一范围内获取多个资源,您将需要本文中精心设计的稳健方法:

http://www.javalobby.org/java/forums/t19048.html

总结如下。

final Connection conn = ...;
try {
    final Statement stmt = ...;
    try {
        final ResultSet rset = ...;
        try {
            //use resources.
        }
        finally {rset.close();}
    }
    finally {stmt.close();}
}
finally {conn.close();}

请阅读原始链接以了解为什么需要这样,以及为什么其他任何内容(例如其他答案中提出的链接)都不够。

With multiple resource acquisitions within the same scope, you would need the approach, which is carefully designed to be robust, within the article:

http://www.javalobby.org/java/forums/t19048.html

which is summarized below.

final Connection conn = ...;
try {
    final Statement stmt = ...;
    try {
        final ResultSet rset = ...;
        try {
            //use resources.
        }
        finally {rset.close();}
    }
    finally {stmt.close();}
}
finally {conn.close();}

Do read the original link to understand why it needs to be this way and why anything else, like the one proposed in the other answers, would not suffice.

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