Skip to content

Commit b67c6ec

Browse files
committed
user thread & daemon thread
1 parent 9afb916 commit b67c6ec

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/main/java/io/concurrency/chapter04/exam03/DaemonThreadLifeCycleExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.concurrency.chapter04.exam03;
22

33
public class DaemonThreadLifeCycleExample {
4+
45
public static void main(String[] args) throws InterruptedException {
56
Thread userThread = new Thread(() -> {
67
try {
@@ -22,7 +23,7 @@ public static void main(String[] args) throws InterruptedException {
2223
}
2324
});
2425

25-
daemonThread.setDaemon(true);
26+
daemonThread.setDaemon(true); //반드시 스레드 실행 전에 데몬으로 지정해야 함
2627
daemonThread.setDaemon(false);
2728

2829
userThread.start();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## 사용자 스레드
2+
3+
모든 사용자 스레드가 종료되어야 애플리케이션 종료!!
4+
이 때 메인 스레드가 먼저 종료되든 나중에 종료되든 상관 X
5+
(메인 스레드도 사용자 스레드니까... 일반적인 사용자 스레드와 다른 점은 JVM이 자동으로 생성한다는 것)
6+
7+
## 부모 스레드 자식 스레드
8+
사용자 스레드는 사용자 스레드를 낳고
9+
데몬 스레드는 데몬 스레드를 낳는다
10+
11+
## 스레드 데몬 설정
12+
- thread.setDaemon(true) 사용
13+
- 반드시 스레드 실행 전에 설정해야 하며, 실행 중에 데몬으로 설정하면 IllegalThreadStateException 발생!!

src/main/java/io/concurrency/chapter04/exam03/UserAndDaemonInheritanceExample.java

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
package io.concurrency.chapter04.exam03;
22

33
public class UserAndDaemonInheritanceExample {
4+
5+
public void test(){
6+
7+
8+
new Thread(() ->{
9+
new Thread(() -> {
10+
System.out.println("사용자 스레드의 자식 스레드 데몬 상태: " + Thread.currentThread().isDaemon());
11+
}).start();
12+
System.out.println("사용자 스레드의 데몬 상태: "+Thread.currentThread().isDaemon());
13+
14+
}).start();
15+
16+
17+
Thread daemonThread = new Thread(() -> {
18+
new Thread(() -> {
19+
System.out.println("데몬 스레드의 자식 스레드 데몬 상태: " + Thread.currentThread().isDaemon());
20+
}).start();
21+
System.out.println("데몬 스레드의 데몬 상태: " + Thread.currentThread().isDaemon());
22+
23+
});
24+
25+
daemonThread.setDaemon(true);
26+
daemonThread.start();
27+
28+
}
429
public static void main(String[] args) {
530

631
// 사용자 스레드 생성

src/main/java/io/concurrency/chapter04/exam03/UserThreadLifecycleExample.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.concurrency.chapter04.exam03;
22

33
public class UserThreadLifecycleExample {
4+
45
public static void main(String[] args) throws InterruptedException {
56
// 사용자 스레드 1 생성
67
Thread userThread1 = new Thread(() -> {
@@ -32,8 +33,8 @@ public static void main(String[] args) throws InterruptedException {
3233
userThread2.start();
3334

3435
// 메인 스레드가 userThread1과 userThread2의 종료를 기다립니다.
35-
// userThread1.join();
36-
// userThread2.join();
36+
userThread1.join();
37+
userThread2.join();
3738

3839
System.out.println("모든 사용자 스레드가 종료되었습니다. 메인 스레드 종료.");
3940
}

0 commit comments

Comments
 (0)