Friday, March 21, 2008

Automatic Resource Management Blocks (ARM) in action

I've been a busy blogger lately. Here's my experiment with Automatic Resource Management Blocks (ARM). First, I will show you how resource management is done in java; then i'll give you the ARM way to do things. To run the ARM example you need the latest cice/arm prototype. Now, let's see the java way.
But before I do, You'll need the following (copy and paste it into a text file and name it "names.txt" (no double quotes):
 
Josh Bloch
Bob Lee
Doug Lea

By the way, the names here are of the authors of the Concise Instance Creation Expression/Automatic Resource Management Blocks proposal. Okay, now that you have the file needed to run this program. Save names.text to the same directory where your class will reside and I assume you know how to compile/run a java program.
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.File;

/**
* Created: Mar 21, 2008 12:46:07 AM
*
* @author Robert O'Connor
*/
public class JavaExample {
BufferedReader r = null;

public JavaExample() {
try {
r = new BufferedReader(new FileReader(new File("names.txt")));
String line;
while ((line = r.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(r!=null)r.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public static void main(String[] args) {
new JavaExample();
}
}

It is overly verbose and cumbersome to clean up resource leaks that could occur. if I eliminated the finally block from my above example and an exception occured, the stream would never be closed and would leak. So we have to use a try-catch-finally to clean up. Remember, finally blocks are always executed. Closing the stream throws an exception so we have to catch that too! Enter Automatic Resource Management Blocks (ARM).

 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
* Created: Mar 21, 2008 12:25:43 AM
*
* @author Robert O'Connor
*/
public class ARMExample {
BufferedReader r = null;

public ARMExample() throws IOException {

do (r = new BufferedReader(new FileReader(new File("names.txt")))) {
String line;
while ((line = r.readLine()) != null) {
System.out.println(line);
}
}
}
public static void main(String[] args) throws IOException {
new ARMExample();
}

}

That is NOT a do-while loop, or a loop at all for that matter. It is the automatic resource management block. As you can see it drastically reduced the boiler plate code, try-catch-finally; it doesn't eliminate unchecked exceptions. You still have to catch what's being thrown and recover from it if possible. The point is to manage your resources automatically, eliminating resource leaks.

1 comment:

SDiZ said...

: do (r = new BufferedReader(new FileReader(new File("names.txt"))))

you would leak a FileReader if "new BuffereReader()" throws (e.g. OutOfMemoryException).

you have to

do (f = new FileReader(new File("names.txt"))) {
do (r = new BufferedReader(f)) {
..
}
}