Skip to content

Commit d3e155c

Browse files
committed
chapter 2 exam02
1 parent b3ed5dc commit d3e155c

7 files changed

+121
-4
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,12 @@ public void run() {
1212

1313
thread.start();
1414

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+
1522
}
1623
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,18 @@ public void run() {
1111
};
1212

1313
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+
1424
}
25+
26+
27+
1528
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ public static void main(String[] args) {
55

66
MyThread myThread = new MyThread();
77
myThread.start();
8+
9+
YwThread thread = new YwThread();
10+
thread.start();
811
}
912
}
1013

14+
class YwThread extends Thread{
15+
@Override
16+
public void run() {
17+
System.out.println(Thread.currentThread().getName() + " : 스레드 실행 중..");
18+
}
19+
}
20+
1121
class MyThread extends Thread{
1222
@Override
1323
public void run() {

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

+9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ public static void main(String[] args) {
66
MyRunnable task = new MyRunnable();
77
Thread thread = new Thread(task);
88
thread.start();
9+
10+
YwRunnable yw = new YwRunnable();
11+
Thread thread2 = new Thread(yw);
12+
thread2.start();
913
}
1014
}
1115

16+
class YwRunnable implements Runnable{
17+
public void run(){
18+
System.out.println(Thread.currentThread().getName() + " : 실행 중..");
19+
}
20+
}
1221
class MyRunnable implements Runnable{
1322

1423
@Override

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

+4
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ public class LambdaThreadExample {
44
public static void main(String[] args) {
55
Thread thread = new Thread(() -> System.out.println(Thread.currentThread().getName() + ": 스레드 실행 중.."));
66
thread.start();
7+
8+
Thread ywThread = new Thread(() -> System.out.println(Thread.currentThread().getName() + " : yw newww 스레드 실행"));
9+
ywThread.start();
10+
711
}
812
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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)

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

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

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+
617
MyRunnable myRunnable = new MyRunnable();
7-
Thread thread = new Thread(new Runnable() {
18+
Thread thread2 = new Thread(new Runnable() {
819
@Override
920
public void run() {
1021
System.out.println(Thread.currentThread().getName() + " :스레드 실행중..");
1122
}
1223
});
1324

14-
thread.start();
15-
// thread.run();
16-
// myRunnable.run();
25+
thread2.start();
1726

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() + ": 메인 스레드 종료");
1838
}
1939

40+
2041
static class MyRunnable implements Runnable{
2142

2243
@Override

0 commit comments

Comments
 (0)