5

In C++ we have the Resource Acquisition Is Initialization (RAII) pattern, which greatly simplifies resource management. The idea is to provide some wrapping object for any kind of resources. The wrapping object's destructor is then responsible for releasing the resources, when it goes out of its scope. For example:

{
    auto_ptr<int> smartPointer = new int;
    // some other code

} // the memory allocated for the int is released automatically
  // by smartPointer's destructor

The most common usage are smart pointers. But, there are many other kinds of resources (files, mutexes, sockets, etc.) which can be managed exactly the same way.

In Java one doesn't have to bother the memory management. But all other types of resources remain. There is finally block, but its usage is quite inconvenient, especially when many different exceptions can be thrown.

So, my question is if there is any Java pattern which provides functionality equivalent to C++ RAII? If not, please share your best practices in this area (instead of the finally, unless it's used some sophisticated way).

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
oo_olo_oo
  • 2,815
  • 5
  • 28
  • 25
  • See this question: http://stackoverflow.com/questions/194261/raii-in-java-is-resource-disposal-always-so-ugly-was-i-had-a-dream – Eclipse Mar 26 '09 at 18:04
  • Also: http://stackoverflow.com/questions/477399/does-java-support-raii-deterministic-destruction – Eclipse Mar 26 '09 at 18:07
  • "which extremely simplify the resource management" are you freaking kidding me?? – hasen Mar 26 '09 at 20:05
  • @hasen j: I don't understand the question. – oo_olo_oo Mar 26 '09 at 20:45
  • 2
    @hasen not sure how resource management could be much simpler than C++ RAII from a source code point of view... – TofuBeer Mar 26 '09 at 21:43
  • Let's imagine some example, which opens 2 files (for reading data intended to be processed and saving processed data), exchanges data with some thread (locking the mutex), and few different user objects can throw. How such a code could look like without the RAII? :-) – oo_olo_oo Mar 26 '09 at 22:09
  • @oo_olo_oo: That's quite an imagination you've got there! :) But seriously, I agree that lack of RAII sucks ass. – Jonas Byström Sep 21 '12 at 14:47
  • I don't see the link to the duplicate question that follows: *"This question already has answers here:"*. I see some in the comments, but I'd like to see the "official" duplicate. – Nagev Aug 24 '21 at 14:42
  • "In Java one doesn't have to bother the memory management. *But all other types of resources remain.*" Would be nice to elaborate on this in the question, i.e. an example where this would be useful in Java even with automatic garbage collection. – Nagev Aug 24 '21 at 14:50

4 Answers4

11

You can use the usual acquire; try { use; } finally { release; }. Alternatively you can abstract the resource handling with the Execute Around idiom.

Community
  • 1
  • 1
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
4

Joshua Bloch has proposed adding a mechanism called Automatic Resource Management to Java as part of Project Coin (small language changes for JDK 7):

  • Which is just (useful) syntactic sugar for try/catch/finally. So if you want to get something done today (as opposed to 2011), see the answers about try/catch/finally. – John M Mar 26 '09 at 19:16
  • 2
    Except that try/finally sucks in comparison to RAII. The original question was whether Java had anything comparable to RAII, and the answer is, apparently, no. – David Thornley Mar 26 '09 at 19:39
3

The nearest equivalent is try/finally, see http://java.sun.com/docs/books/tutorial/essential/exceptions/finally.html

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
1

To many coders, the strength of the RAII idiom is that the life of the underlying resource is tied to a scope block, making things simpler to make and maintain; ultimately reducing errors from failing to release that resource.

Unfortunately you can't mimic this behaviour in Java as you can't create your own scope-bound structures. A language similar to Java that has tried to solve this problem is C#:

// explicit release
MyClass obj = MyClass();
obj.UseIt();
obj.Dispose();

// RAII-like (scope-bound) release
using(MyClass obj = new MyClass())
{
    obj.UseIt();
}

Perhaps we'll see functionality like this implemented in future.

jond
  • 29
  • 4