1 min read
Managing the life cycle of resources in Java 7: the new try-with-resources block
Alex Campos : December 5, 2012
Java 7 includes a small language change to handle the destruction of resources in a try statement. The new code block, known as try-with-resources, looks like this:
try (MyResource myResource = new MyResource()) {
// use myResource
}
MyResource is a class that implements java.lang.AutoCloseable. The Java Virtual Machine will call myResource.close() whenever the program leaves the try block (whether it exits normally or after throwing an exception).
Using well-known patterns for managing the lifecycle of resources helps in writing code that is readable, robust and maintainable. Until now, Java has lacked a consistent, built-in mechanism to do this. Programming in a garbage-collected language like Java also requires the programmer to manually dispose some objects such as files, sockets or database handlers. And relying on the garbage collector to do this (by implementing the finalize() method) is generally a bad idea--the garbage collector is non-deterministic! The most common way to handle this before Java 7 would be a try-finally block:
MyResource myResource = new MyResource();
try {
// use myResource
} finally {
myResource.close();
}
Other languages had resolved this problem before. In C++ it's common to use the idiom known as "Resource Acquisition is Initialization" (RAII). RAII means that the programmer associates a new object in the stack with any resource requiring clean-up. The destructor of this object takes care of releasing the resource. Another example is C#, where programmers already benefited from the using statement, almost identical to the new Java try-with-resources block.
Here at RTI we realized that this little new Java feature goes a long way toward making your code cleaner. In the Request-Reply API, part of RTI Connext Messaging, you can wrap resources that you need to return to the middleware in a try-with-resources block. For example, reading loaned data from a Requester is as simple as:
import com.rti.connext.infrastructure.*;
import com.rti.connext.requestreply.*;
import myTypeSupport.RequestType;
import myTypeSupport.ReplyType;
Requester<RequestType, ReplyType> requester = ...;
RequestType request = ...;
requester.sendRequest(request);
try (Sample.Iterator<ReplyType> dataSamples = requester.receiveReplies()) {
while(dataSamples.hasNext()) {
Sample<ReplyType> dataSample = dataSamples.next();
System.out.println("Received a new reply: " + dataSample.getData());
}
}
When your application exits the try block, it automatically calls the Sample.Iterator.close() operation, which then returns the loaned data to the middleware (the equivalent to DataReader.return_loan(), in the Data Distribution Service (DDS) API).
Simple and elegant.
Posts by Tag
- Developers/Engineer (303)
- Connext DDS Suite (186)
- IIoT (125)
- News & Events (122)
- Standards & Consortia (122)
- Technology (74)
- Leadership (73)
- 2020 (54)
- Automotive (49)
- Aerospace & Defense (48)
- 2023 (35)
- Cybersecurity (33)
- Culture & Careers (31)
- Healthcare (31)
- 2022 (29)
- Connext DDS Tools (21)
- 2021 (19)
- Connext DDS Pro (19)
- Energy Systems (16)
- Military Avionics (15)
- FACE (13)
- Connext DDS Micro (12)
- 2024 (10)
- JADC2 (10)
- ROS 2 (10)
- Transportation (9)
- Connectivity Technology (7)
- Connext DDS Cert (7)
- Databus (7)
- Oil & Gas (5)
- Connext Conference (4)
- Connext DDS (4)
- RTI Labs (4)
- Case + Code (3)
- FACE Technical Standard (3)
- Research (3)
- Robotics (3)
- #A&D (2)
- Edge Computing (2)
- MDO (2)
- MS&T (2)
- Other Markets (2)
- TSN (2)
- ABMS (1)
- C4ISR (1)
- ISO 26262 (1)
- L3Harris (1)
- LabView (1)
- MathWorks (1)
- National Instruments (1)
- Simulation (1)
- Tech Talks (1)
- UAM (1)
- Videos (1)
- eVTOL (1)