์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 29 | 30 |
- ๊ฑฐ๋ญ์ ๊ณฑ
- fibonacci
- ๊นํ๋ธ
- ๋ฐฑ์ค์๊ณ ๋ฆฌ์ฆ
- ๋ฌธ์์ด๋ค์ง๊ธฐ
- CLI๋ช ๋ น์ด
- ํ์ดํ๋ก๊ทธ๋๋ฐ
- Spring Security
- ๊ทธ๋ฆฌ๋
- CSS
- ํ๊ณ
- ์๊ณ ๋ฆฌ์ฆ
- FilterChain
- ์ธํ ๋ฆฌ์ ์ด
- ์๋ฐ
- ๊ณ์ฐ๊ธฐ๋ง๋ค๊ธฐ
- ๋ถํธ์บ ํ
- ์ปฌ๋ ์ ํ๋ ์์ํฌ
- testing
- Publishing
- ์ฒซ๊ธ์๋๋ฌธ์
- Spring Data JDBC
- ๋ฐ์ผ๋ฆฌ์ฝ๋ฉ
- java
- ์คํ๋ง
- ์๋ฃ๊ตฌ์กฐ
- ์ ๋ค๋ฆญ์ค
- HTML
- ๋ฐฑ์๋
- spring data jpa
- Today
- Total
๋์ ๋ชจ์
019 | Java - I/O, Thread, JVM ๋ณธ๋ฌธ
๐ File I/O
๐ค InputStream / OutputStream
- File ์
์ถ๋ ฅ ์คํธ๋ฆผ ⇒ ๋ฐ์ดํธ ๊ธฐ๋ฐ
- ๋ฐ์ดํธ๊ธฐ๋ฐ ⇒ ์ ์ถ๋ ฅ๋จ์: 1byte
- ์คํธ๋ฆผ์ ๋จ๋ฐฉํฅ์ผ๋ก๋ง ๋ฐ์ดํฐ๋ฅผ ์ ์ก ⇒ ์ ์ถ๋ ฅ ๋์์ ์ฒ๋ฆฌ ⇒ ๊ฐ๊ฐ์ ์คํธ๋ฆผ์ด ํ์
- ์๋ฐ์์ ์
์ถ๋ ฅ์ ๋ค๋ฃจ๊ธฐ ⇒ InputStream / OutputStream
- ์ ์ถ๋ ฅ ์คํธ๋ฆผ์ ์ด๋ค ๋์์ ๋ค๋ฃจ๋๋์ ๋ฐ๋ผ ์ข ๋ฅ๊ฐ ๋๋ฉ๋๋ค.
- File์ ๋ค๋ฃฐ ๋: FileInputStream / FileOutputStream ์ฌ์ฉ
- ํ๋ก์ธ์ค๋ฅผ ๋ค๋ฃฐ ๋: PipedInputStream / PipedOutputStream ์ฌ์ฉ
FileInputStream
echo FileInputStream >> fis.txt
- ๋ณด์กฐ์คํธ๋ฆผ์ธ BufferedInputStream ์ฌ์ฉํ๋ฉด ์ฑ๋ฅ ํฅ์
- ๋ฒํผ == ๋ฐ์ดํธ๋ฐฐ์ด
- ์ฌ๋ฌ ๋ฐ์ดํธ๋ฅผ ์ ์ฅํ์ฌ ํ๋ฒ์ ๋ง์ ์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ถ๋ ฅํ ์ ์๋๋ก ๋์์ฃผ๋ ์์ ์ ์ฅ ๊ณต๊ฐ
- ๋ฒํผ == ๋ฐ์ดํธ๋ฐฐ์ด
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamEx {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("/practice/fis.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int i = 0;
while((i = fis.read()) != -1) { // fis.read()์ ๋ฆฌํด๊ฐ์ i์ ์ ์ฅํ ํ ๊ฐ์ด -1์ธ์ง ํ์ธ
System.out.println((char)i);
}
fis.close();
} catch (Exception e) {
System.out.println("์๋ฌ: " + e.getMessage());
}
}
}
/*์ถ๋ ฅ ๊ฒฐ๊ณผ
practice ํด๋ ์์ FileInputStream์ด๋ผ๋ ํ
์คํธ๊ฐ ๋ค์ด์๋ fis.txt๋ผ๋ ํ
์คํธ ํ์ผ์ด ์์ฑ๋จ
*/
FileOutputStream
import java.io.FileOutputStream;
public class FileOutputStreamEx {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("/practice/fos.txt");
String word = "FileOutStream";
byte b[] = word.getBytes();
fos.write(b);
fos.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
/*์ถ๋ ฅ ๊ฒฐ๊ณผ
practice ํด๋ ์์ FileOutStream์ด๋ผ๋ ํ
์คํธ๊ฐ ๋ค์ด์๋ fos.txt๋ผ๋ ํ
์คํธ ํ์ผ์ด ์์ฑ๋จ
*/
๐ค FileReader / FileWriter
- ๋ฌธ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ: ๋ฌธ์ ๋ฐ์ดํฐ ๋ค๋ฃฐ ๋ ์ฌ์ฉ
- ์ ๋์ฝ๋(UTF-16)๊ฐ์ ๋ณํ์ ์๋์ผ๋ก ์ฒ๋ฆฌ
- ๋ฌธ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ์์๋
- InputStream → Reader / OutputStream → Writer
- FileInputStream → FileReader
- FileOutputStream→ FileWriter
- FileReader: ์ธ์ฝ๋ฉ → ์ ๋์ฝ๋
- FileWriter๋: ์ ๋์ฝ๋ → ์ธ์ฝ๋ฉ
FileReader
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderEx {
public static void main(String[] args) {
try {
String fileName = "/practice/fis.txt";
// FileReader file = new FileReader(fileName);
FileInputStream file = new FileInputStream(fileName);
int data = 0;
while((data = file.read()) != -1) {
System.out.println((char)data);
}
file.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
- ํ๊ธ ์ ๋ ฅ ํ FileReader → FileInputStream ๋ณ๊ฒฝ ⇒ ํ๊ธ ๊นจ์ง
- ์ฑ๋ฅ ๊ฐ์ : BufferedReader
FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterEx {
public static void main(String[] args) {
try {
String fileName = "/Users/keson/practice/practice.txt";
FileWriter writer = new FileWriter(fileName);
String str = "written";
writer.write(str);
writer.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
/* ์ถ๋ ฅ ๊ฒฐ๊ณผ
practice.txt ํ์ผ์ written ๋จ์ด ์จ์ง
*/
๐ค File
- File ํด๋์ค๋ก ํ์ผ๊ณผ ๋๋ ํ ๋ฆฌ์ ์ ๊ทผ ๊ฐ๋ฅ
import java.io.File;
import java.io.IOException;
public class FileEx {
public static void main(String[] args) throws IOException {
File file = new File("../practice.txt");
System.out.println(file.getPath());
System.out.println(file.getParent());
System.out.println(file.getCanonicalPath());
System.out.println(file.canWrite());
}
}
- ํ์ผ ์ธ์คํด์ค ์์ฑ != ํ์ผ ์์ฑ
ํ์ผ์ ์์ฑ: ํ์ผ ์ธ์คํด์ค๋ฅผ ์์ฑ ์→ ์ฒซ ๋ฒ์งธ ์ธ์์ ๊ฒฝ๋ก → ๋ ๋ฒ์งธ ์ธ์์ ํ์ผ๋ช → createNewFile() ๋ฉ์๋ ํธ์ถ
์์
ํ์ฌ ๋๋ ํ ๋ฆฌ(.)์์ ํ์ฅ์๊ฐ .txt์ธ ํ์ผ๋ช ์์ “code”๋ผ๋ ๋ฌธ์์ด ๋ถ์ฌ์ฃผ๊ธฐ
import java.io.File;
public class FileNameEx {
public static void main(String[] args) {
File parentDir = new File("./");
File[] list = parentDir.listFiles();
String prefix = "code";
for(int i = 0; i < list.length; i++) {
String fileName = list[i].getName();
if(fileName.endsWith("txt") && !fileName.startsWith("code")) {
list[i].renameTo(new File(parentDir, prefix + fileName));
}
}
}
}
โ Ref.
- FileInputStream
InputStream (Java Platform SE 7 )
Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown. If
docs.oracle.com
- FileOutputStream
OutputStream (Java Platform SE 7 )
Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such b
docs.oracle.com
- reader
Reader (Java Platform SE 7 )
reset public void reset() throws IOException Resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for ex
docs.oracle.com
- writer
Writer (Java SE 11 & JDK 11 )
Returns a new Writer which discards all characters. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect. While the stream is open, the append(char), append(CharSequence), app
docs.oracle.com
- File ๋ฉ์๋ ํ์ธ
File (Java Platform SE 7 )
Returns the absolute pathname string of this abstract pathname. If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the path
docs.oracle.com
๐ Thread
๐ค ์ฐ๋ ๋
ํ๋ก์ธ์ค์ ์ฐ๋ ๋
- ํ๋ก๊ทธ๋จ: ์คํ ๊ฐ๋ฅํ ํ์ผ
- ํ๋ก์ธ์ค: ์คํ์ค์ธ ํ๋ก๊ทธ๋จ
- ๊ตฌ์ฑ์์: ๋ฐ์ดํฐ, ์ปดํจํฐ ์์, ์ค๋ ๋
- ์ฐ๋ ๋: ํ๋ก์ธ์ค์์ ์คํ๋๋ ์์ค์ฝ๋์ ์คํํ๋ฆ
- ์ฑ๊ธ ์ฐ๋ ๋ ํ๋ก์ธ์ค: ํ๋์ ์ฐ๋ ๋๋ฅผ ๊ฐ์ง๋ ํ๋ก์ธ์ค
- ๋ฉํฐ ์ฐ๋ ๋ ํ๋ก์ธ์ค: ์ฌ๋ฌ ๊ฐ์ ์ฐ๋ ๋๋ฅผ ๊ฐ์ง๋ ํ๋ก์ธ์ค
๋ฉ์ธ์ฐ๋ ๋
- ์๋ฐ๋ฅผ ์คํํ๋ฉด ๊ฐ์ฅ ๋จผ์ ์คํ๋๋ ๋ฉ์๋ ⇒ main ๋ฉ์๋ ์คํ
- ์ฝ๋์ ๋์ด๋ return๋ฌธ์์ ์คํ ์ข ๋ฃ
๋ฉํฐ์ฐ๋ ๋
- ์ฌ๋ฌ ๊ฐ์ ์ฐ๋ ๋๋ฅผ ๊ฐ์ง ์ ์๋ ํ๋ก์ธ์ค
- ๋ฉํฐ ์ฐ๋ ๋ฉ: ์ฌ๋ฌ ์ฐ๋ ๋๊ฐ ๋์์ ์์ ์ ์ํํ ์ ์๋ ๊ฒ
๐ค ์ฐ๋ ๋์ ์์ฑ๊ณผ ์คํ
์์ ์ฐ๋ ๋์ ์์ฑ๊ณผ ์คํ
- ๋ณ๋์ ์์
์ฐ๋ ๋๋ฅผ ํ์ฉํ๋ค๋ ๊ฒ?
⇒ ์์ ์ฐ๋ ๋๊ฐ ์ํํ ์ฝ๋๋ฅผ ์์ฑํ๊ณ , ์์ ์ฐ๋ ๋๋ฅผ ์์ฑํ์ฌ ์คํ์ํค๋ ๊ฒ
run() ๋ฉ์๋๋ก ์์ ์ฐ๋ ๋๋ฅผ ์์ฑํ๊ณ ์คํํ๋ ๋ฐฉ๋ฒ
- Runnable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด์์ run()์ ๊ตฌํํ์ฌ ์ค๋ ๋๋ฅผ ์์ฑํ๊ณ ์คํ
public class ThreadEx1 {
public static void main(String[] args) {
// 4. ์ฐ๋ ๋ ์์ฑ
// 4-1. Runnable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด ํ์ฉ
Runnable task1 = new ThreadTask1();
// 4-2.Runnable ๊ตฌํ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌํ๋ฉด์ Thread ํด๋์ค๋ฅผ ์ธ์คํด์คํํด์ ์ฐ๋ ๋ ์์ฑ
Thread thread1 = new Thread(task1);
// 4-1, 4-2 ํ์ค๋ก
// Thread thread1 = new Thread(new ThreadTask1());
// 5. ์์
์ฐ๋ ๋ ์คํ -> run() ๋ด๋ถ ์ฝ๋ ์ฒ๋ฆฌ
thread1.start();
// 6. ๋ฐ๋ณต๋ฌธ ์ถ๊ฐ
for(int i = 0; i < 50; i++) {
System.out.println("@");
}
}
}
// 1. Runnable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ํด๋์ค ์์ฑ
class ThreadTask1 implements Runnable {
// 2. run() ๋ฉ์๋ ์์ฑ
public void run() {
// 3. ์ฐ๋ ๋๊ฐ ์ํํ ์์
๋ด์ฉ ์์ฑ
for(int i = 0; i < 50; i++) {
System.out.println("#");
}
}
}
- Thread ํด๋์ค๋ฅผ ์์ ๋ฐ์ ํ์ ํด๋์ค์์ run()์ ๊ตฌํํ์ฌ ์ค๋ ๋๋ฅผ ์์ฑํ๊ณ ์คํ
public class ThreadEx2 {
public static void main(String[] args) {
// 4. Thread ํด๋์ค๋ฅผ ์์๋ฐ์ ํด๋์ค๋ฅผ ์ธ์คํด์คํํ์ฌ ์ฐ๋ ๋ ์์ฑ
Thread thread2 = new ThreadTask2();
// 5. ์์
์ฐ๋ ๋ ์คํ -> run() ๋ด๋ถ ์ฝ๋ ์ฒ๋ฆฌ
thread2.start();
// 6. ๋ฐ๋ณต๋ฌธ ์ถ๊ฐ
for(int i = 0; i < 100; i++) {
System.out.println("@");
}
}
}
// 1. Thread ํด๋์ค ์์๋ฐ๋ ํด๋์ค ์์ฑ
class ThreadTask2 extends Thread {
// 2. run() ๋ฉ์๋ ์์ฑ
public void run() {
// 3. ์ฐ๋ ๋๊ฐ ์ํํ ์์
๋ด์ฉ ์์ฑ
for(int i = 0; i < 100; i++) {
System.out.println("#");
}
}
}
⇒ ๋ ๋ฐฉ๋ฒ ๋ค ์์ ์ฐ๋ ๋๋ฅผ ๋ง๋ค๊ณ ๋์ผํ ๋ด๋ถ ๋์ ์ํ(run() ๋ฉ์๋์ ์์ฑ๋ ์ฝ๋ ์ฒ๋ฆฌ)
์ต๋ช ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ฐ๋ ๋์ ์์ฑ๊ณผ ์คํ
- Runnable ์ต๋ช ๊ตฌํ ๊ฐ์ฒด๋ฅผ ํ์ฉํ ์ฐ๋ ๋ ์์ฑ ๋ฐ ์คํ
public class ThreadEx3 {
public static void main(String[] args) {
// ์ต๋ช
Runnable ๊ตฌํ ๊ฐ์ฒด๋ฅผ ํ์ฉํ ์ฐ๋ ๋ ์์ฑ
Thread thread3 = new Thread(new Runnable() {
public void run() {
for(int i = 0; i < 100; i++) {
System.out.print("#");
}
}
});
thread3.start();
for(int i = 0; i < 100; i++) {
System.out.print("@");
}
}
}
- Thread ์ต๋ช ํ์ ๊ฐ์ฒด๋ฅผ ํ์ฉํ ์ค๋ ๋ ์์ฑ ๋ฐ ์คํ
public class ThreadEx4 {
public static void main(String[] args) {
// ์ต๋ช
Thread ํ์ ๊ฐ์ฒด๋ฅผ ํ์ฉํ ์ฐ๋ ๋ ์์ฑ
Thread thread4 = new Thread() {
public void run() {
for(int i = 0; i < 100; i++) {
System.out.print("#");
}
}
};
thread4.start();
for(int i = 0; i < 100; i++) {
System.out.print("@");
}
}
}
๐ค ์ฐ๋ ๋ ๋๊ธฐํ
- ํ ๋ฒ์ ํ๋์ ์ฐ๋ ๋๋ง ๊ฐ์ฒด์ ์ ๊ทผํ ์ ์๋๋ก ๊ฐ์ฒด์ ๋ฝ(lock)์ ๊ฑธ์ด ๋ฐ์ดํฐ์ ์ผ๊ด์ฑ์ ์ ์งํ๋ ๊ฒ
์๊ณ ์์ญ(Critical section) / ๋ฝ(Lock)
- ์๊ณ ์์ญ: ๋จ ํ๋์ ์ฐ๋ ๋๋ง ์ฝ๋๋ฅผ ์คํํ ์ ์๋ ์ฝ๋ ์์ญ
- ๋ฝ: ์๊ณ ์์ญ์ ํฌํจํ๊ณ ์๋ ๊ฐ์ฒด์ ์ ๊ทผํ ์ ์๋ ๊ถํ
๋ฉ์๋ ์ ์ฒด๋ฅผ ์๊ณ์์ญ์ผ๋ก ์ง์
- ๋ฉ์๋ ๋ฐํํ์ ์ผ์ชฝ์ synchronized ๋ถ์ฌ์ฃผ๋ฉด, ๋ฉ์๋๊ฐ ํธ์ถ๋์์ ๋ ๋ฉ์๋๋ฅผ ์คํํ ์ค๋ ๋๋ ๋ฉ์๋๊ฐ ํฌํจ๋ ๊ฐ์ฒด์ ๋ฝ์ ์ป์
ํน์ ์์ญ์ ์๊ณ์์ญ์ผ๋ก ์ง์
- synchronized ํค์๋์ ํจ๊ป ์๊ดํธ(()) ์์ ํด๋น ์์ญ์ด ํฌํจ๋ ๊ฐ์ฒด์ ์ฐธ์กฐ๋ฅผ ๋ฃ์
- ์ค๊ดํธ({})๋ก ๋ธ๋ญ์ ์ด์ด ๋ธ๋ญ ๋ด์ ์ฝ๋๋ฅผ ์์ฑ
โ Ref.
๐ JVM(Java Virtual Machine)
JVM
- ์๋ฐ๋ก ์์ฑํ ์์ค ์ฝ๋๋ฅผ ํด์ํด ์คํํ๋ ๋ณ๋์ ํ๋ก๊ทธ๋จ
JVM ๊ตฌ์กฐ

JVM ๋ฉ๋ชจ๋ฆฌ๊ตฌ์กฐ

Stack ์์ญ
- Stack: ์ผ์ข ์ ์๋ฃ๊ตฌ์กฐ(ํ๋ก๊ทธ๋จ์ด ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ฐฉ์)
- LIFO(Last In First Out): ๋ง์ง๋ง์ ๋ค์ด๊ฐ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ฅ ๋จผ์ ๋์ด
Heap ์์ญ
- JVM์ด ์๋๋๋ฉด ์๋ ์์ฑ๋๋ ์์ญ์ผ๋ก, ๊ฐ์ฒด๋ ์ธ์คํด์ค ๋ณ์, ๋ฐฐ์ด์ด ์ ์ฅ
Piano piano = new Piano();
- new Piano() ์คํ→ Heap ์์ญ์ ์ธ์คํด์ค๊ฐ ์์ฑ → ์ธ์คํด์ค๊ฐ ์์ฑ๋ ์์น์ ์ฃผ์๊ฐ์ piano์ ํ ๋น
⇒ piano๋ Stack ์์ญ์ ์ ์ธ๋ ๋ณ์ - ๊ฐ์ฒด๋ฅผ ๋ค๋ฃฌ๋ค๋ ๊ฒ?์ ๋ฆฌํ์๋ฉด, Heap ์์ญ์ ์ค์ ๊ฐ์ฒด์ ๊ฐ์ด ์ ์ฅ๋๋ ๊ณต๊ฐ
⇒ Stack ์์ญ์ ์ ์ฅ๋์ด ์๋ ์ฐธ์กฐ ๋ณ์๋ฅผ ํตํด Heap ์์ญ์ ์กด์ฌํ๋ ๊ฐ์ฒด๋ฅผ ๋ค๋ฃจ๋ ๊ฒ
Garbage Collection
- ํ๋ก๊ทธ๋จ์์ ๋ ์ด์ ์ฌ์ฉํ์ง ์๋ ๊ฐ์ฒด๋ฅผ ์ฐพ์ ์ญ์ ํ๊ฑฐ๋ ์ ๊ฑฐํ์ฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ๋ณดํ๋ ๋ฉ๋ชจ๋ฆฌ ์๋ ๊ด๋ฆฌ ํ๋ก์ธ์ค
โ Ref.
Stack Memory and Heap Space in Java | Baeldung
Explore how Stack Memory and Heap Space works and when to use them for developing better Java programs.
www.baeldung.com
JVM์ ์ด๋ค ์ญํ ์ ํ ๊น?
JVM ์ด๋ ๋ฌด์์ผ๊น? JVM์ด๋ Java Virtual Machine ์ ์ค์๋ง๋ก, Java Byte Code ๋ฅผ ์ด์์ฒด์ ์ ๋ง๊ฒ ํด์ํด์ฃผ๋ ์ญํ ์ ํฉ๋๋ค. ์ฆ, ์์ฑํ ์๋ฐ ํ๋ก๊ทธ๋จ์ ์คํ ํ๊ฒฝ์ ์ ๊ณตํ๋ ์๋ฐ ํ๋ก๊ทธ๋จ์ ๊ตฌ๋ ์
velog.io
๐ ์ค๋์ ์๊ฐ ์กฐ๊ฐ๋ชจ์
- ๐ญ
'SEB > TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
024 | Data Structure - Tree, Graph (0) | 2022.07.26 |
---|---|
023 | Data Structure - Stack & Queue (0) | 2022.07.25 |
018 | Java - Enum, Annotation, Lambda, Stream (0) | 2022.07.18 |
017 | Java - ๐ฅ Practical | Collection Framework (0) | 2022.07.16 |
016 | Java - Generics, Collection Framework (2) | 2022.07.15 |