Notice
Recent Posts
Recent Comments
| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Applepay
- ํ ํฐํธ์ด
- ๋ฒ ์ด์ง์
- ํ ํฐํ์ทจ
- ๋์ฌ์จ์ด
- ์ คํ
- ์๋ฐ
- ์ ํํ์ด
- ์๋์ฐ11
- ์์
- ํ ์ค๋ฑ ํฌ
- 0์๋ฉ๊ฐํจ์ค
- ํ์ด์ค๋ถ์๋ช ์ด๋
- Ai
- ์ปจํ๋ฆฌ์ค
- ๋ค์์๊ณ ๋ผ์๋ช ์ด๋
- ChatGPT
- ๋ฐฉ๋ฌธ์์ถ์ ๊ธฐ
- wannacry
- ๋ฉํ๋ฆฌ
- 2017 ํฐ์คํ ๋ฆฌ ๊ฒฐ์ฐ
- ๋ด๋ฐฉ๋ฌธ์์ฐพ์์ฃผ์ค
- perplexity
- ์ดํด๋ฆฝ์ค
- ํฐ์คํ ๋ฆฌ
- ํ์ด์ค๋ถ
- 2017ํฐ์คํ ๋ฆฌ๊ฒฐ์ฐ
- ์ทจ์ฝ์
- zelf
- ๋ค์์๊ณ ๋ผ์ฌ์นญ
Archives
- Today
- Total
EdgeSecu
JAVA-๋คํธ์ํฌ ์์ ์ ๋ฆฌ ๋ณธ๋ฌธ
๐งฝ ๊ฐ๋ ์ ๋ฆฌ ๋จผ์ : AutoCloseable, try-with-resources, finally
1. AutoCloseable์ด๋?
- ์์ ์ ๋ฆฌ ์๋ํ๋ฅผ ์ํ ์ธํฐํ์ด์ค
- close()๋ผ๋ ๋ฉ์๋ ํ๋๋ง ๊ฐ์ ํจ
public interface AutoCloseable {
void close() throws Exception;
}
- ์๋ฐ์ InputStream, OutputStream, Socket, BufferedReader, Connection, ์ ๋ถ ๊ตฌํํ๊ณ ์์.
- ๋ชฉ์ ์ ๋จ ํ๋:
- "์ด ๊ฐ์ฒด๊ฐ ๋๋ ๋ ์์์ ์ ๋ฆฌ๋๊ฒ ๋ง๋ค์ด์ค์~"
2. try-with-resources๋?
- AutoCloseable์ ๊ตฌํํ ๊ฐ์ฒด๋ฅผ try ๋ธ๋ก ์์์ ์ ์ธํ๋ฉด
- try ๋ธ๋ก์ด ๋๋๋ฉด ์๋์ผ๋ก close()๊ฐ ํธ์ถ๋จ
→ ์์ธ๊ฐ ๋๋ ๋ง๋ ๋ฅ์น๊ณ closeํจ
try (Socket socket = new Socket("localhost", 8080)) {
// socket ์ฌ์ฉ
} // ์ฌ๊ธฐ์ ์๋์ผ๋ก socket.close() ํธ์ถ๋จ
๐ก ์ด๊ฒ ์๋ฐ 7์์ ๋์ ๋ ๋ฌธ๋ฒ์. ์ ์ธ๊ณ์๋ค ์ง์ง๋ก.
3. finally ๋ธ๋ก์์ ์๋์ผ๋ก ๋ซ๊ธฐ
์๋ฐ 7 ์ด์ ์๋ ์ ๋ถ ์ด๋ ๊ฒ ํ์ด์ผ ๋จ. ์ฌํ์ ์๋.
Socket socket = null;
try {
socket = new Socket("localhost", 8080);
// ์ฌ์ฉ
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// ์๋ฌ ๋ฌด์
}
}
}
๐ ๋ฒ๊ฑฐ๋กญ๊ณ , try ์์ ๋ try ์๋ ๊ฑธ ๋ณด๋ฉด ๋๋ฌผ์ด ๋จ
๐งช ์ ๋ฆฌ: ์ ์ ๊ด๊ณ
- AutoCloseable = "์๋ ์ ๋ฆฌ ๊ฐ๋ฅ" ์คํฌํ. ๋ด๊ฐ ์ง์ ๋ง๋ค ์๋ ์์.
- try-with-resources = "AutoCloseable ๊ตฌํ ๊ฐ์ฒด๋ ์์์ ์ ๋ฆฌํด์ค๊ฒ์" ๋ผ๋ ๋ฌธ๋ฒ
- finally = ์๋ฐ 7 ์ด์ ์ ์๋ ๋ ธ๋. ์ง๊ธ๋ ํน์ ์ํฉ์์ ์ฐ๊ธด ํจ (๋ก๊ทธ ๋จ๊ธธ ๋ ๋ฑ)
๐ง AutoCloseable ์ง์ ๊ตฌํ ์์
public class MyConnection implements AutoCloseable {
public void connect() {
System.out.println("์ฐ๊ฒฐ ์์!");
}
@Override
public void close() {
System.out.println("์์ ์ ๋ฆฌ๋จ. ์ฐ๊ฒฐ ์ข
๋ฃ!");
}
}
์ฌ์ฉ ์:
try (MyConnection conn = new MyConnection()) {
conn.connect();
// ์์
์ํ
} // ์ฌ๊ธฐ์ ์๋์ผ๋ก close() ํธ์ถ
๐ ์ฌ๊ธฐ์ ์ค์ํ ํฌ์ธํธ:
- close()๋ ์์ธ๋ฅผ ๋์ง ์ ์์ (throws Exception)
- try-with-resources๋ ์ฌ๋ฌ ์์ ๋์์ ๊ด๋ฆฌ ๊ฐ๋ฅ
try (InputStream in = new FileInputStream("input.txt");
OutputStream out = new FileOutputStream("output.txt")) {
// ํ์ผ ๋ณต์ฌ
}
๐ ๋ง๋ฌด๋ฆฌ ํ
- ๋คํธ์ํฌ ์์ (Socket, ServerSocket ๋ฑ)์ ํญ์ ๋ซ์์ผ ํจ. ์ ๋ซ์ผ๋ฉด ํฌํธ ๋จน๊ณ ์ ๋๋๋ค.
- try-with-resources๊ฐ ์์ธ ์ฒ๋ฆฌ + ์์ ์ ๋ฆฌ ๋์์ ํด์ฃผ๋ฏ๋ก ๋ฌด์กฐ๊ฑด ์ด ๋ฐฉ์ ์ถ์ฒ.
- ๋ค๊ฐ ์ง์ ๋ง๋ ์์์ ์ ๋ฆฌํ๊ณ ์ถ๋ค๋ฉด, ๊ทธ๋ฅ AutoCloseable ๊ตฌํํด๋ฒ๋ฆฌ๋ฉด ๋จ.
'๊ณต๋ถ > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| static์ ๊ดํ์ฌ (0) | 2025.05.17 |
|---|---|
| final์ ๊ดํ์ฌ (0) | 2025.05.17 |
| JAVA try-with-resources(์๋ ์์ ํด์ ) (0) | 2025.05.13 |
| ๋ฐฉ์ด์ ํ๋ก๊ทธ๋๋ฐ์ ์ค์์ฑ: TaskScheduler์ null ์ฒดํฌ(MID2-9-๋ฌธ์ ์ ํ์ด4-๋ฌธ์ 2) (0) | 2025.03.29 |
| [์์ ]๊ฐ์ฅ ํฐ ์ ์ฐพ๊ธฐ (0) | 2017.04.29 |