Java one time mode

C # supports the one - time mode of deterministic garbage collection using dispose pattern

Does java have such a pattern?

Java 7 has autoclosable, and you can call the close method with the try finally block

How about the version before 7?

Does Java 5 or 6 have a one-time mode (deterministic garbage collection)?

Solution

Before Java 7, the closest thing was the "manual" try / finally block:

FileInputStream input = new FileInputStream(...);
try {
  // Use input
} finally {
  input.close();
}

When I first started using c#1.0 from a Java background, using statements was one of the best c# I found Nice to see it in Java 7:)

You should also consider using closables in guava – it allows you to worry about whether the reference is empty (just like the using statement), and you can throw "logs and swaps" exceptions when closing to avoid exceptions thrown by the effective "overwrite" try block

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>