2023. 1. 25. 13:19ใJAVA/Effective JAVA
item08. finalizer์ cleaner ์ฌ์ฉ์ ํผํ๋ผ.
" p43. AutoClosable "
AutoClosable์ด๋?
: try-with-resources๋ฅผ ์ง์ํ๋ ์ธํฐํ์ด์ค์ด๋ค.
/*
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.lang;
/**
* An object that may hold resources (such as file or socket handles)
* until it is closed. The {@link #close()} method of an {@code AutoCloseable}
* object is called automatically when exiting a {@code
* try}-with-resources block for which the object has been declared in
* the resource specification header. This construction ensures prompt
* release, avoiding resource exhaustion exceptions and errors that
* may otherwise occur.
*
* @apiNote
* <p>It is possible, and in fact common, for a base class to
* implement AutoCloseable even though not all of its subclasses or
* instances will hold releasable resources. For code that must operate
* in complete generality, or when it is known that the {@code AutoCloseable}
* instance requires resource release, it is recommended to use {@code
* try}-with-resources constructions. However, when using facilities such as
* {@link java.util.stream.Stream} that support both I/O-based and
* non-I/O-based forms, {@code try}-with-resources blocks are in
* general unnecessary when using non-I/O-based forms.
*
* @author Josh Bloch
* @since 1.7
*/
public interface AutoCloseable {
/**
* Closes this resource, relinquishing any underlying resources.
* This method is invoked automatically on objects managed by the
* {@code try}-with-resources statement.
*
* <p>While this interface method is declared to throw {@code
* Exception}, implementers are <em>strongly</em> encouraged to
* declare concrete implementations of the {@code close} method to
* throw more specific exceptions, or to throw no exception at all
* if the close operation cannot fail.
*
* <p> Cases where the close operation may fail require careful
* attention by implementers. It is strongly advised to relinquish
* the underlying resources and to internally <em>mark</em> the
* resource as closed, prior to throwing the exception. The {@code
* close} method is unlikely to be invoked more than once and so
* this ensures that the resources are released in a timely manner.
* Furthermore it reduces problems that could arise when the resource
* wraps, or is wrapped, by another resource.
*
* <p><em>Implementers of this interface are also strongly advised
* to not have the {@code close} method throw {@link
* InterruptedException}.</em>
*
* This exception interacts with a thread's interrupted status,
* and runtime misbehavior is likely to occur if an {@code
* InterruptedException} is {@linkplain Throwable#addSuppressed
* suppressed}.
*
* More generally, if it would cause problems for an
* exception to be suppressed, the {@code AutoCloseable.close}
* method should not throw it.
*
* <p>Note that unlike the {@link java.io.Closeable#close close}
* method of {@link java.io.Closeable}, this {@code close} method
* is <em>not</em> required to be idempotent. In other words,
* calling this {@code close} method more than once may have some
* visible side effect, unlike {@code Closeable.close} which is
* required to have no effect if called more than once.
*
* However, implementers of this interface are strongly encouraged
* to make their {@code close} methods idempotent.
*
* @throws Exception if this resource cannot be closed
*/
void close() throws Exception;
}
AutoClosable ์ close ๋ฉ์๋ ํ๋๋ง์ ๊ฐ์ง ์ธํฐํ์ด์ค์ด์ง๋ง ๊ทธ๋ ๋ค๊ณ ํจ์ํ ์ธํฐํ์ด์ค๋ ์๋๋ค. (๊ทธ๋ด ๋ชฉ์ ์ผ๋ก ๋ง๋ ์ธํฐํ์ด์ค๊ฐ ์๋)
๊ทธ๋ฆฌ๊ณ close ๋ฉ์๋๋ Exception ์๋ฌ๋ฅผ ๋์ง๊ณ ์๋ค.
try-with-resources๋ฅผ ์ง์ํ๋ ์ธํฐํ์ด์ค๋ผ๋ ๊ฒ์ ์ฆ ์์ํด์ ๋ฅผ ํ ๋ ์ฌ์ฉ๋๋ ์ธํฐํ์ด์ค๋ก
์์ ํด์ ๋๊ธธ ๋ฐ๋ผ๋ ํด๋์ค๋ผ๋ฉด AutoCloseable์ implementํด์ ๊ตฌํํด์ฃผ๋ฉด ๋๋ค.
public class AutoClosableIsGood implements AutoCloseable{
private BufferedInputStream inputStream;
@Override
public void close(){
// idempotent ํด์ผ ํ๋ค.(๋ช๋ฒ์ ํธ์ถํ๋๋ผ๋ ๋์ผํ ๊ฒฐ๊ณผ๊ฐ ์ํ๋์ด์ผ ํ๋ค.) AutoClosable ์ ๊ถ์ฅ, Closeable ํ์
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException("failed to close " + inputStream);
}
}
}
๊ทธ๋ฆฌ๊ณ ์ฌ๊ธฐ์ BufferedInputStream์ ์ฌ์ฉํ๊ณ ์๊ธฐ ๋๋ฌธ์ IOException์ด ๋ฐ์ํ ํ๋ฅ ์ด ์๊ธฐ ๋๋ฌธ์ Exception ๋ณด๋ค ๊ตฌ์ฒด์ ์ธ IOException์ ์ก์์คฌ๋ค. ์ด๋ ๊ฒ ์ฌ๊ธฐ์ ์์ธ๋ฅผ ์ก๊ณ ๋ฐํ์ ์์ธ๋ก ๋ณํํด์ ๋์ ธ์ฃผ๋ฉด ํด๋ผ์ด์ธํธ ์ฝ๋์์ ์์ธ ์ฒ๋ฆฌ๋ฅผ ์ํด์ค๋ ๋๊ธฐ ๋๋ฌธ์ ์ด ๋ฐฉ๋ฒ์ ๊ถ์ฅํ๋ค.
public class App {
public static void main(String[] args) {
try (AutoClosableIsGood good = new AutoClosableIsGood()){
// TODO ์์ ๋ฐ๋ฉ ์ฒ๋ฆฌ๊ฐ ๋จ
}
}
}
๊ทธ๋ฆฌ๊ณ ํด๋ผ์ด์ธํธ ์ฝ๋์์๋ ์ด๋ฐ์์ผ๋ก ์ฌ์ฉํด์ฃผ๋ฉด ๋๋ค.
Closable
: AutoClosable์ ๊ตฌํํ ์ธํฐํ์ด์ค
AutoClosable๋ณด๋ค ์ข ๋ ๊ตฌ์ฒด์ ์ธ ํด๋์ค๋ก AutoCloseable์ Exception์ ๋์ง๋ ๋ฐ๋ฉด, Closeable์ IOException์ ๋์ง๋ค.
๊ทธ๋ฆฌ๊ณ AutoCloseable์ idempotent๋ฅผ ๊ถ์ฅํ์ง๋ง, Closeable์ ๋ฐ๋์ idempotent ํด์ผ ํ๋ค.
โidempotent๋?
๋ฒ์ญํ๋ฉด '๋ฉฑ๋ฑ์ฑ' ์ด๋ผ๊ณ ์ข ๋ ์ด๋ ค์ด๋ง๋ก ๋ฒ์ญ๋๋๋ฐ ..
์ฐ์ฐ์ ์ฌ๋ฌ ๋ฒ ์ ์ฉํ๋๋ผ๋ ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ง์ง ์๋ ์ฑ์ง, ์ฐ์ฐ์ ์ฌ๋ฌ ๋ฒ ๋ฐ๋ณตํ์ฌ๋ ํ ๋ฒ๋ง ์ํ๋ ๊ฒ๊ณผ ๊ฐ์ ์ฑ์ง์ ์๋ฏธํ๋ค.
'JAVA > Effective JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ดํํฐ๋ธ ์๋ฐ] Item09 ์๋ฒฝ๊ณต๋ต. (0) | 2023.01.25 |
---|---|
[์ดํํฐ๋ธ ์๋ฐ] Item9. try-finally ๋ณด๋ค๋ try-with-resources ๋ฅผ ์ฌ์ฉํ๋ผ. (0) | 2023.01.25 |
[์ดํํฐ๋ธ ์๋ฐ] Item08 ์๋ฒฝ๊ณต๋ต. Finalizer ๊ณต๊ฒฉ (0) | 2023.01.25 |
[์ดํํฐ๋ธ ์๋ฐ] Item08 ์๋ฒฝ๊ณต๋ต (0) | 2023.01.25 |
[์ดํํฐ๋ธ ์๋ฐ] Item8. finalizer์ cleaner ์ฌ์ฉ์ ํผํ๋ผ. (0) | 2023.01.22 |