#107 RAII example JavaDoc
This commit is contained in:
parent
57a271d4fe
commit
ace8b3e22f
@ -5,27 +5,32 @@ package com.iluwatar.resource.acquisition.is.initialization;
|
|||||||
* Resource Acquisition Is Initialization pattern was developed
|
* Resource Acquisition Is Initialization pattern was developed
|
||||||
* for exception safe resource management by C++ creator Bjarne
|
* for exception safe resource management by C++ creator Bjarne
|
||||||
* Stroustrup.
|
* Stroustrup.
|
||||||
*
|
* <p>
|
||||||
* In RAII resource is tied to object lifetime: resource allocation
|
* In RAII resource is tied to object lifetime: resource allocation
|
||||||
* is done during object creation while resource deallocation is
|
* is done during object creation while resource deallocation is
|
||||||
* done during object destruction.
|
* done during object destruction.
|
||||||
*
|
* <p>
|
||||||
* In Java RAII is achieved with try-with-resources statement and
|
* In Java RAII is achieved with try-with-resources statement and
|
||||||
* interfaces Closeable and AutoCloseable. The try-with-resources
|
* interfaces {@link Closeable} and {@link AutoCloseable}. The try-with-resources
|
||||||
* statement ensures that each resource is closed at the end of the
|
* statement ensures that each resource is closed at the end of the
|
||||||
* statement. Any object that implements java.lang.AutoCloseable, which
|
* statement. Any object that implements {@link java.lang.AutoCloseable}, which
|
||||||
* includes all objects which implement java.io.Closeable, can be used
|
* includes all objects which implement {@link java.io.Closeable}, can be used
|
||||||
* as a resource.
|
* as a resource.
|
||||||
*
|
*
|
||||||
* In this example, SlidingDoor implements AutoCloseable and
|
* In this example, {@link SlidingDoor} implements {@link AutoCloseable} and
|
||||||
* TreasureChest implements Closeable. Running the example, we can
|
* {@link TreasureChest} implements {@link Closeable}. Running the example, we can
|
||||||
* observe that both resources are automatically closed.
|
* observe that both resources are automatically closed.
|
||||||
*
|
* <p>
|
||||||
* http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html
|
* http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Program entry point
|
||||||
|
* @param args command line args
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public static void main( String[] args ) throws Exception {
|
public static void main( String[] args ) throws Exception {
|
||||||
|
|
||||||
try (SlidingDoor slidingDoor = new SlidingDoor()) {
|
try (SlidingDoor slidingDoor = new SlidingDoor()) {
|
||||||
|
@ -4,6 +4,11 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.iluwatar.resource.acquisition.is.initialization.App;
|
import com.iluwatar.resource.acquisition.is.initialization.App;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Application test
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user