File tree 5 files changed +55
-33
lines changed
main/java/io/concurrency/chapter02/exam02
5 files changed +55
-33
lines changed Original file line number Diff line number Diff line change 2
2
3
3
public class MultiThreadAppTerminatedExample {
4
4
public static void main (String [] args ) {
5
- System .out .println ("프로그램 시작" );
6
5
7
- // 멀티스레드 실행
8
6
for (int i = 0 ; i < 3 ; i ++) {
9
- Thread thread = new Thread (new MyRunnable (i ));
7
+ Thread thread = new Thread (new ThreadStackExample . MyRunnable (i ));
10
8
thread .start ();
11
9
}
12
10
13
11
System .out .println ("메인 스레드 종료" );
12
+
14
13
}
14
+ static class MyRunnable implements Runnable {
15
15
16
- private static class MyRunnable implements Runnable {
17
- private int threadId ;
16
+ private final int threadId ;
18
17
19
18
public MyRunnable (int threadId ) {
19
+
20
20
this .threadId = threadId ;
21
21
}
22
22
23
23
@ Override
24
24
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 );
29
39
}
30
40
}
31
41
}
Original file line number Diff line number Diff line change 1
1
package io .concurrency .chapter02 .exam02 ;
2
2
3
3
public class SingleThreadAppTerminatedExample {
4
-
5
4
public static void main (String [] args ) {
6
- System .out .println ("프로그램 시작" );
7
5
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 ;
11
9
}
12
10
13
- System .out .println ("프로그램 종료" );
14
- }
11
+ System .out .println ("sum : " + sum );
15
12
13
+ System .out .println ("메인 스레드 종료" );
14
+
15
+ }
16
16
}
Original file line number Diff line number Diff line change 1
1
package io .concurrency .chapter02 .exam02 ;
2
2
3
3
public class ThreadStackExample {
4
-
5
4
public static void main (String [] args ) {
6
- // 스레드를 3개 생성하고 시작
5
+
7
6
for (int i = 0 ; i < 3 ; i ++) {
8
7
Thread thread = new Thread (new MyRunnable (i ));
9
8
thread .start ();
10
9
}
10
+
11
11
}
12
+ static class MyRunnable implements Runnable {
12
13
13
- private static class MyRunnable implements Runnable {
14
- private int threadId ;
14
+ private final int threadId ;
15
15
16
16
public MyRunnable (int threadId ) {
17
+
17
18
this .threadId = threadId ;
18
19
}
19
20
20
21
@ Override
21
22
public void run () {
23
+ System .out .println (Thread .currentThread ().getName () + ": 스레드 실행 중..." );
22
24
firstMethod (threadId );
23
25
}
24
26
25
- private void firstMethod (int id ) {
26
- int localValue = id + 100 ; // 지역 변수
27
+ private void firstMethod (int threadId ) {
28
+
29
+ int localValue = threadId + 100 ;
27
30
secondMethod (localValue );
31
+
28
32
}
29
33
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 );
33
37
}
34
38
}
35
-
36
39
}
Original file line number Diff line number Diff line change 3
3
public class ThreadStartRunExample {
4
4
public static void main (String [] args ) {
5
5
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
+
8
14
thread .start ();
9
15
// thread.run();
10
- // task.run();
16
+ // myRunnable.run();
17
+
11
18
}
12
- }
13
19
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
+ }
17
26
}
18
27
}
You can’t perform that action at this time.
0 commit comments