Skip to content

Commit a962e87

Browse files
committed
lecture
1 parent 0d695fa commit a962e87

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

src/main/java/io/concurrency/chapter02/exam01/ExtendThreadExample.java

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public static void main(String[] args) {
88

99
MyThread thread2 = new MyThread();
1010
thread2.start();
11-
1211
// new MyThread().start();
1312
}
1413
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.concurrency.chapter02.exam02;
2+
3+
public class MultiThreadAppTerminatedExample {
4+
public static void main(String[] args) {
5+
System.out.println("프로그램 시작");
6+
7+
// 멀티스레드 실행
8+
for (int i = 0; i < 3; i++) {
9+
Thread thread = new Thread(new MyRunnable(i));
10+
thread.start();
11+
}
12+
13+
System.out.println("메인 스레드 종료");
14+
}
15+
16+
private static class MyRunnable implements Runnable {
17+
private int threadId;
18+
19+
public MyRunnable(int threadId) {
20+
this.threadId = threadId;
21+
}
22+
23+
@Override
24+
public void run() {
25+
for (int i = 0; i < 5; i++) {
26+
System.out.println("스레드 " + threadId + " 작업 진행: " + i);
27+
}
28+
System.out.println("스레드 " + threadId + " 종료");
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.concurrency.chapter02.exam02;
2+
3+
public class SingleThreadAppTerminatedExample {
4+
5+
public static void main(String[] args) {
6+
System.out.println("프로그램 시작");
7+
8+
// 단순한 작업 실행
9+
for (int i = 0; i < 5; i++) {
10+
System.out.println("작업 진행: " + i);
11+
}
12+
13+
System.out.println("프로그램 종료");
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.concurrency.chapter02.exam02;
2+
3+
public class ThreadStackExample {
4+
5+
public static void main(String[] args) {
6+
// 스레드를 3개 생성하고 시작
7+
for (int i = 0; i < 3; i++) {
8+
Thread thread = new Thread(new MyRunnable(i));
9+
thread.start();
10+
}
11+
}
12+
13+
private static class MyRunnable implements Runnable {
14+
private int threadId;
15+
16+
public MyRunnable(int threadId) {
17+
this.threadId = threadId;
18+
}
19+
20+
@Override
21+
public void run() {
22+
firstMethod(threadId);
23+
}
24+
25+
private void firstMethod(int id) {
26+
int localValue = id + 100; // 지역 변수
27+
secondMethod(localValue);
28+
}
29+
30+
private void secondMethod(int value) {
31+
String localVar = "스레드 ID: " + threadId + ", Value: " + value;
32+
System.out.println(localVar);
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)