File tree 7 files changed +121
-4
lines changed
src/main/java/io/concurrency/chapter02
7 files changed +121
-4
lines changed Original file line number Diff line number Diff line change @@ -12,5 +12,12 @@ public void run() {
12
12
13
13
thread .start ();
14
14
15
+ Thread ywThread = new Thread (new Runnable () {
16
+ public void run () {
17
+ System .out .println (Thread .currentThread ().getName () + " : yw스레드 실행중.." );
18
+ }
19
+ });
20
+ ywThread .start ();
21
+
15
22
}
16
23
}
Original file line number Diff line number Diff line change @@ -11,5 +11,18 @@ public void run() {
11
11
};
12
12
13
13
thread .start ();
14
+
15
+
16
+ Thread ywThread = new Thread () {
17
+ public void run () {
18
+ System .out .println (Thread .currentThread ().getName () + " : yw 스레드 실행중 .." );
19
+ }
20
+ };
21
+
22
+ ywThread .start ();
23
+
14
24
}
25
+
26
+
27
+
15
28
}
Original file line number Diff line number Diff line change @@ -5,9 +5,19 @@ public static void main(String[] args) {
5
5
6
6
MyThread myThread = new MyThread ();
7
7
myThread .start ();
8
+
9
+ YwThread thread = new YwThread ();
10
+ thread .start ();
8
11
}
9
12
}
10
13
14
+ class YwThread extends Thread {
15
+ @ Override
16
+ public void run () {
17
+ System .out .println (Thread .currentThread ().getName () + " : 스레드 실행 중.." );
18
+ }
19
+ }
20
+
11
21
class MyThread extends Thread {
12
22
@ Override
13
23
public void run () {
Original file line number Diff line number Diff line change @@ -6,9 +6,18 @@ public static void main(String[] args) {
6
6
MyRunnable task = new MyRunnable ();
7
7
Thread thread = new Thread (task );
8
8
thread .start ();
9
+
10
+ YwRunnable yw = new YwRunnable ();
11
+ Thread thread2 = new Thread (yw );
12
+ thread2 .start ();
9
13
}
10
14
}
11
15
16
+ class YwRunnable implements Runnable {
17
+ public void run (){
18
+ System .out .println (Thread .currentThread ().getName () + " : 실행 중.." );
19
+ }
20
+ }
12
21
class MyRunnable implements Runnable {
13
22
14
23
@ Override
Original file line number Diff line number Diff line change @@ -4,5 +4,9 @@ public class LambdaThreadExample {
4
4
public static void main (String [] args ) {
5
5
Thread thread = new Thread (() -> System .out .println (Thread .currentThread ().getName () + ": 스레드 실행 중.." ));
6
6
thread .start ();
7
+
8
+ Thread ywThread = new Thread (() -> System .out .println (Thread .currentThread ().getName () + " : yw newww 스레드 실행" ));
9
+ ywThread .start ();
10
+
7
11
}
8
12
}
Original file line number Diff line number Diff line change
1
+ # ThreadStartRunExample
2
+
3
+ 스레드는 start()를 통해서 실행시켜야 한다!<br >
4
+ run()은 JVM이 자동으로 호출하는 실행 메서드이기 때문에 직접 스레드에 run()을 호출해주게 되면 그냥 메인 스레드 스택에 run()이 생성되며, 별도의 스레드가 생성되지 않는다<br >
5
+
6
+ ``` java
7
+
8
+ public static void main(String [] args) {
9
+
10
+ Thread thread1 = new Thread (new Runnable () {
11
+ @Override
12
+ public void run () {
13
+ System . out. println(Thread . currentThread(). getName() + " : 스레드 실행중.." );
14
+ }
15
+ });
16
+
17
+ Thread thread2 = new Thread (new Runnable () {
18
+ @Override
19
+ public void run () {
20
+ System . out. println(Thread . currentThread(). getName() + " : 스레드 실행중.." );
21
+ }
22
+ });
23
+
24
+ MyRunnable my = new MyRunnable ();
25
+ Thread thread3 = new Thread (my);
26
+
27
+ thread1. start();
28
+ thread2. start();
29
+ thread3. run();
30
+
31
+
32
+ System . out. println(Thread . currentThread(). getName() + " : 메인 스레드 종료" );
33
+ }
34
+
35
+ static class MyRunnable implements Runnable {
36
+ @Override
37
+ public void run () {
38
+ System . out. println(Thread . currentThread(). getName() + " : 스레드 실행 중.." );
39
+ }
40
+ }
41
+
42
+ ```
43
+ 지금 start()와 run()을 같이 사용해주고 있다<br >
44
+ 출력 결과는 다음과 같다
45
+ ``` java
46
+ Thread - 1 : 스레드 실행중..
47
+ Thread - 0 : 스레드 실행중..
48
+ main: 스레드 실행 중...
49
+ main: 메인 스레드 종료
50
+ Thread - 2 : 스레드 실행중..
51
+ ```
52
+ - 스레드는 독립적으로 동작하기 때문에 메인 스레드가 종료된 후에도 새로운 스레드가 생성됨을 확인 가능
53
+ - thread3.run()을 했기 때문에 main 스레드에서 run()이 동작했음을 확인 가능 (즉 새로운 스레드 생성 x)
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
+
7
+ Thread thread1 = new Thread (new Runnable () {
8
+ @ Override
9
+ public void run () {
10
+ System .out .println (Thread .currentThread ().getName () + " : 스레드 실행중.." );
11
+ }
12
+ });
13
+
14
+
15
+ thread1 .start ();
16
+
6
17
MyRunnable myRunnable = new MyRunnable ();
7
- Thread thread = new Thread (new Runnable () {
18
+ Thread thread2 = new Thread (new Runnable () {
8
19
@ Override
9
20
public void run () {
10
21
System .out .println (Thread .currentThread ().getName () + " :스레드 실행중.." );
11
22
}
12
23
});
13
24
14
- thread .start ();
15
- // thread.run();
16
- // myRunnable.run();
25
+ thread2 .start ();
17
26
27
+ myRunnable .run ();
28
+
29
+ Thread thread3 = new Thread (new Runnable () {
30
+ @ Override
31
+ public void run () {
32
+ System .out .println (Thread .currentThread ().getName () + " : 스레드 실행중.." );
33
+ }
34
+ });
35
+
36
+ thread3 .start ();
37
+ System .out .println (Thread .currentThread ().getName () + ": 메인 스레드 종료" );
18
38
}
19
39
40
+
20
41
static class MyRunnable implements Runnable {
21
42
22
43
@ Override
You can’t perform that action at this time.
0 commit comments