Skip to content

Commit f72c610

Browse files
committed
lecture
1 parent 4bf4c15 commit f72c610

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

src/main/java/io/concurrency/chapter02/exam02/MultiThreadAppTerminatedExample.java

+19-9
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,40 @@
22

33
public class MultiThreadAppTerminatedExample {
44
public static void main(String[] args) {
5-
System.out.println("프로그램 시작");
65

7-
// 멀티스레드 실행
86
for (int i = 0; i < 3; i++) {
9-
Thread thread = new Thread(new MyRunnable(i));
7+
Thread thread = new Thread(new ThreadStackExample.MyRunnable(i));
108
thread.start();
119
}
1210

1311
System.out.println("메인 스레드 종료");
12+
1413
}
14+
static class MyRunnable implements Runnable{
1515

16-
private static class MyRunnable implements Runnable {
17-
private int threadId;
16+
private final int threadId;
1817

1918
public MyRunnable(int threadId) {
19+
2020
this.threadId = threadId;
2121
}
2222

2323
@Override
2424
public void run() {
25-
for (int i = 0; i < 5; i++) {
26-
System.out.println("스레드 " + threadId + " 작업 진행: " + i);
27-
}
28-
System.out.println("스레드 " + threadId + " 종료");
25+
System.out.println(Thread.currentThread().getName() + ": 스레드 실행 중...");
26+
firstMethod(threadId);
27+
}
28+
29+
private void firstMethod(int threadId) {
30+
31+
int localValue = threadId + 100;
32+
secondMethod(localValue);
33+
34+
}
35+
36+
private void secondMethod(int localValue) {
37+
String objectReference = threadId + ": Hello World";
38+
System.out.println(Thread.currentThread().getName() + " : 스레드 ID : " + threadId + ", Value:" + localValue);
2939
}
3040
}
3141
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package io.concurrency.chapter02.exam02;
22

33
public class SingleThreadAppTerminatedExample {
4-
54
public static void main(String[] args) {
6-
System.out.println("프로그램 시작");
75

8-
// 단순한 작업 실행
9-
for (int i = 0; i < 5; i++) {
10-
System.out.println("작업 진행: " + i);
6+
int sum = 0;
7+
for (int i = 0; i <1000; i++) {
8+
sum += i;
119
}
1210

13-
System.out.println("프로그램 종료");
14-
}
11+
System.out.println("sum : " + sum);
1512

13+
System.out.println("메인 스레드 종료");
14+
15+
}
1616
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
package io.concurrency.chapter02.exam02;
22

33
public class ThreadStackExample {
4-
54
public static void main(String[] args) {
6-
// 스레드를 3개 생성하고 시작
5+
76
for (int i = 0; i < 3; i++) {
87
Thread thread = new Thread(new MyRunnable(i));
98
thread.start();
109
}
10+
1111
}
12+
static class MyRunnable implements Runnable{
1213

13-
private static class MyRunnable implements Runnable {
14-
private int threadId;
14+
private final int threadId;
1515

1616
public MyRunnable(int threadId) {
17+
1718
this.threadId = threadId;
1819
}
1920

2021
@Override
2122
public void run() {
23+
System.out.println(Thread.currentThread().getName() + ": 스레드 실행 중...");
2224
firstMethod(threadId);
2325
}
2426

25-
private void firstMethod(int id) {
26-
int localValue = id + 100; // 지역 변수
27+
private void firstMethod(int threadId) {
28+
29+
int localValue = threadId + 100;
2730
secondMethod(localValue);
31+
2832
}
2933

30-
private void secondMethod(int value) {
31-
String localVar = "스레드 ID: " + threadId + ", Value: " + value;
32-
System.out.println(localVar);
34+
private void secondMethod(int localValue) {
35+
String objectReference = threadId + ": Hello World";
36+
System.out.println(Thread.currentThread().getName() + " : 스레드 ID : " + threadId + ", Value:" + localValue);
3337
}
3438
}
35-
3639
}

src/main/java/io/concurrency/chapter02/exam02/ThreadStartRunExample.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@
33
public class ThreadStartRunExample {
44
public static void main(String[] args) {
55

6-
MyRunnable task = new MyRunnable();
7-
Thread thread = new Thread(task);
6+
MyRunnable myRunnable = new MyRunnable();
7+
Thread thread = new Thread(new Runnable() {
8+
@Override
9+
public void run() {
10+
System.out.println(Thread.currentThread().getName() + " :스레드 실행중..");
11+
}
12+
});
13+
814
thread.start();
915
// thread.run();
10-
// task.run();
16+
// myRunnable.run();
17+
1118
}
12-
}
1319

14-
class MyRunnable implements Runnable {
15-
public void run() {
16-
System.out.println("스레드 실행 중");
20+
static class MyRunnable implements Runnable{
21+
22+
@Override
23+
public void run() {
24+
System.out.println(Thread.currentThread().getName() + ": 스레드 실행 중...");
25+
}
1726
}
1827
}

src/강의자료.pptx

1.32 KB
Binary file not shown.

0 commit comments

Comments
 (0)