Skip to content

Commit 34608da

Browse files
committed
feat: ch12 정리
1 parent 2c131a3 commit 34608da

File tree

2 files changed

+436
-0
lines changed

2 files changed

+436
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.example.concurrency;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
6+
public class JavaOneToOneThreadTest {
7+
8+
public static void main(String[] args) throws InterruptedException {
9+
long pid = ProcessHandle.current().pid();
10+
String osName = System.getProperty("os.name").toLowerCase();
11+
12+
System.out.println("PID: " + pid);
13+
System.out.println();
14+
15+
System.out.println("=== 초기 상태 ===");
16+
int initialOsThreads = getThreadCount(pid, osName);
17+
int initialJavaThreads = Thread.activeCount();
18+
System.out.println("Java Thread 수: " + initialJavaThreads);
19+
System.out.println("OS Thread 수: " + initialOsThreads);
20+
System.out.println();
21+
22+
System.out.println("=== 5개 Thread 생성 테스트 ===");
23+
for (int i = 1; i <= 5; i++) {
24+
final int id = i;
25+
new Thread(() -> {
26+
System.out.println("Thread-" + id + " 생성");
27+
try {
28+
while (true) Thread.sleep(1000);
29+
} catch (InterruptedException e) {
30+
Thread.currentThread().interrupt();
31+
}
32+
}).start();
33+
Thread.sleep(200);
34+
}
35+
36+
Thread.sleep(2000);
37+
38+
System.out.println();
39+
System.out.println("=== 테스트 완료 후 ===");
40+
int finalOsThreads = getThreadCount(pid, osName);
41+
int finalJavaThreads = Thread.activeCount();
42+
System.out.println("Java Thread 수: " + finalJavaThreads);
43+
System.out.println("OS Thread 수: " + finalOsThreads);
44+
System.out.println();
45+
46+
int javaIncrease = finalJavaThreads - initialJavaThreads;
47+
int osIncrease = finalOsThreads - initialOsThreads;
48+
49+
System.out.println("=== 증가량 분석 ===");
50+
System.out.println("생성한 Thread: 5개");
51+
System.out.println("Main Thread: 1개 (이미 실행 중)");
52+
System.out.println("Java Thread 증가: " + javaIncrease + "개");
53+
System.out.println("OS Thread 증가: " + osIncrease + "개");
54+
System.out.println();
55+
56+
System.out.println("=== 1:1 매핑 검증 ===");
57+
printMappingResult(javaIncrease, osIncrease);
58+
59+
Thread.sleep(30000);
60+
}
61+
62+
private static void printMappingResult(int javaIncrease, int osIncrease) {
63+
System.out.println("Java Thread 증가: " + javaIncrease);
64+
System.out.println("OS Thread 증가: " + osIncrease);
65+
System.out.println();
66+
67+
if (javaIncrease == osIncrease) {
68+
System.out.println("결과: PERFECT 1:1 매핑!");
69+
System.out.println("┌─ Java Thread ─┐ ┌─ OS Thread ─┐");
70+
System.out.println("│ " + javaIncrease + "개 │ ←→ │ " + osIncrease + "개 │");
71+
System.out.println("└───────────────┘ └─────────────┘");
72+
} else {
73+
System.out.println("결과: 차이 발생 (" + (osIncrease - javaIncrease) + "개)");
74+
System.out.println("┌─ Java Thread ─┐ ┌─ OS Thread ─┐");
75+
System.out.println("│ " + javaIncrease + "개 │ ≠ │ " + osIncrease + "개 │");
76+
System.out.println("└───────────────┘ └─────────────┘");
77+
System.out.println("차이 원인: JVM 내부 Thread 추가 생성");
78+
}
79+
}
80+
81+
private static int getThreadCount(long pid, String osName) {
82+
try {
83+
String command;
84+
if (osName.contains("windows")) {
85+
command = "wmic process where \"ProcessId=" + pid + "\" get ThreadCount /format:value";
86+
} else if (osName.contains("mac")) {
87+
command = "ps -M " + pid + " | wc -l";
88+
} else {
89+
command = "ps -T -p " + pid + " | wc -l";
90+
}
91+
92+
Process process = Runtime.getRuntime().exec(command);
93+
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
94+
95+
String line;
96+
while ((line = reader.readLine()) != null) {
97+
if (osName.contains("windows")) {
98+
if (line.startsWith("ThreadCount=")) {
99+
return Integer.parseInt(line.split("=")[1].trim());
100+
}
101+
} else {
102+
try {
103+
int count = Integer.parseInt(line.trim());
104+
if (count > 0) return count - 1;
105+
} catch (NumberFormatException ignored) {}
106+
}
107+
}
108+
reader.close();
109+
110+
} catch (Exception e) {
111+
System.out.println("명령어 실행 오류: " + e.getMessage());
112+
}
113+
114+
return -1;
115+
}
116+
}

0 commit comments

Comments
 (0)