Skip to content

Commit e5b7ce9

Browse files
committed
name() & currentThread() & isAlive()
1 parent c35125f commit e5b7ce9

9 files changed

+101
-20
lines changed

src/main/java/io/concurrency/chapter03/exam03/InterruptExample.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public static void main(String[] args) throws InterruptedException {
1818
Thread.sleep(1000);
1919
thread1.start();
2020

21-
thread1.join();
22-
thread2.join();
21+
thread1.join(); //메인 스레드는 thread1 작업이 끝날 때 까지 대기
22+
thread2.join(); //메인 스레드는 thread2 작업이 끝날 때 까지 대기
2323

2424
System.out.println("모든 스레드 작업 완료");
2525
}

src/main/java/io/concurrency/chapter03/exam03/InterruptedExample1.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ public static void main(String[] args) {
77
System.out.println("스레드가 작동 중입니다.");
88
}
99
System.out.println("스레드가 인터럽트 되었습니다.");
10+
//인터럽트 상태를 초기화 시킴
1011
System.out.println("인터럽트 상태: " + Thread.currentThread().isInterrupted());
1112
});
1213
thread.start();
1314

1415
try {
15-
Thread.sleep(1000);
16+
Thread.sleep(5000);
1617
} catch (InterruptedException e) {
1718
e.printStackTrace();
1819
}
1920

20-
thread.interrupt();
21+
thread.interrupt(); //메인스레드에서 thread에게 인터럽트 발생
2122
}
2223
}

src/main/java/io/concurrency/chapter03/exam03/InterruptedExample2.java

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static void main(String[] args) {
1313
for (int i = 0; i < 5; i++) {
1414
System.out.println("스레드 1 작동 중");
1515
if (i == 2) {
16+
System.out.println("i = " + i);
1617
thread2.interrupt();
1718
}
1819
try {

src/main/java/io/concurrency/chapter03/exam03/InterruptedExample3.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public class InterruptedExample3 {
44
public static void main(String[] args) {
5+
6+
57
Thread thread = new Thread(() -> {
68
while (true) {
79
System.out.println("스레드 작동 중");

src/main/java/io/concurrency/chapter03/exam03/InterruptedExceptionExample.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
public class InterruptedExceptionExample {
44
public static void main(String[] args) throws InterruptedException {
5-
Thread thread = new Thread(() -> {
5+
6+
Thread thread1 = new Thread(() -> {
67
try {
78
System.out.println("인터럽트 상태 1: " + Thread.currentThread().isInterrupted());
89
Thread.sleep(5000);
@@ -14,16 +15,16 @@ public static void main(String[] args) throws InterruptedException {
1415
}
1516
});
1617

17-
thread.start();
18+
thread1.start();
1819

1920
try {
2021
Thread.sleep(2000);
2122
} catch (InterruptedException e) {
2223
e.printStackTrace();
2324
}
2425

25-
thread.interrupt();
26-
thread.join();
27-
System.out.println("인터럽트 상태 3: " + thread.isInterrupted());
26+
thread1.interrupt();
27+
thread1.join(); //메인 스레드가 thread1에 조인. 즉 thread1의 작업이 끝날 때까지 대기
28+
System.out.println("인터럽트 상태 3: " + thread1.isInterrupted());
2829
}
2930
}

src/main/java/io/concurrency/chapter03/exam03/IsInterruptedExample.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
public class IsInterruptedExample {
44
public static void main(String[] args) {
55
Thread thread = new Thread(() -> {
6-
while (!Thread.currentThread().isInterrupted()) {
7-
System.out.println("스레드가 작동 중입니다.");
6+
while(!Thread.currentThread().isInterrupted()){
7+
System.out.println("스레드가 작동 중입니다..");
88
}
9-
System.out.println("스레드가 인터럽트 되었습니다.");
10-
System.out.println("인터럽트 상태: " + Thread.currentThread().isInterrupted());
9+
System.out.println("스레드가 인터럽트 되었습니다");
10+
System.out.println("인터럽트 상태 : "+Thread.currentThread().isInterrupted());
1111
});
1212
thread.start();
1313

@@ -17,6 +17,6 @@ public static void main(String[] args) {
1717
e.printStackTrace();
1818
}
1919

20-
thread.interrupt();
20+
thread.interrupt(); //메인 스레드가 thread에게 인터럽트 발생시킴
2121
}
2222
}

src/main/java/io/concurrency/chapter03/exam04/CurrentThreadExample.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
public class CurrentThreadExample {
44
public static void main(String[] args) {
55

6+
67
// 현재 실행 중인 스레드를 가져와 이름 출력
7-
System.out.println("현재 스레드: " + Thread.currentThread());
8-
System.out.println("현재 스레드 이름: " + Thread.currentThread().getName());
8+
System.out.println("현재 스레드(main): " + Thread.currentThread());
9+
System.out.println("현재 스레드 이름(main): " + Thread.currentThread().getName());
910

1011
// Thread 객체를 사용하여 스레드 생성 및 시작
1112
Thread thread1 = new Thread() {
1213
@Override
1314
public void run() {
14-
System.out.println("현재 스레드: " + Thread.currentThread());
15+
System.out.println("현재 스레드 (Thread 객체 사용): " + this);
1516
// 현재 스레드의 이름을 출력
1617
System.out.println("현재 스레드 이름 (Thread 객체 사용): " + getName());
1718
}
@@ -22,11 +23,12 @@ public void run() {
2223
thread2.start();
2324
}
2425

26+
2527
static class ThreadName implements Runnable {
2628

2729
@Override
2830
public void run() {
29-
System.out.println("현재 스레드: " + Thread.currentThread());
31+
System.out.println("현재 스레드 (Runnable 사용): " + Thread.currentThread());
3032
// 현재 스레드의 이름을 출력
3133
System.out.println("현재 스레드 이름 (Runnable 사용): " + Thread.currentThread().getName());
3234
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# ThreadNamingExample
2+
3+
```java
4+
public class ThreadNamingExample {
5+
public static void main(String[] args) throws InterruptedException {
6+
7+
8+
// 스레드 이름을 생성자에 전달하여 설정
9+
Thread myThread = new Thread(() -> {
10+
System.out.println("현재 스레드 이름: " + Thread.currentThread().getName());
11+
}, "myThread");
12+
myThread.start();
13+
14+
// setName() 메서드를 사용하여 스레드 이름 설정
15+
Thread yourThread = new Thread(() -> {
16+
System.out.println("현재 스레드 이름: " + Thread.currentThread().getName());
17+
});
18+
yourThread.setName("yourThread");
19+
yourThread.start();
20+
21+
// 여러 개의 스레드를 생성하여 기본 스레드 이름을 출력
22+
for (int i = 0; i < 5; i++) {
23+
Thread defaultThread = new Thread(() -> {
24+
System.out.println("현재 스레드 이름: " + Thread.currentThread().getName());
25+
});
26+
defaultThread.start();
27+
}
28+
29+
Thread.sleep(2000);
30+
}
31+
}
32+
```
33+
- 스레드의 이름을 지정하지 않으면 Thread-0...Thread-n까지 이렇게 0부터 시작해서 이름을 부여하는데,
34+
- 위 경우 yourThread는 처음에 이름을 지정하지 않고 생성되었기 떄문에 Thread-0이라는 이름을 가지는데, setNamed()을 통해 이름을 수정함
35+
- 따라서 이미 한번 nextThreadNum()이 호출되었기 때문에 threadSeqNumber가 1의 값을 갖고 있고, 이후 defaultThread는 Thread-1부터 시작해서 이름을 부여받게 됨
36+
- 생성자에 이름을 같이 전달하면 nextThreadNum()이 호출되지 않고 바로 이름 부여됨
37+
38+
# CurrentThreadExample
39+
스레드의 이름을 가져오는 3가지 경우를 살펴본다
40+
41+
1. 현재 실행 중인 스레드 정보 가져오기
42+
```java
43+
// 현재 실행 중인 스레드를 가져와 이름 출력
44+
System.out.println("현재 스레드(main): " + Thread.currentThread());
45+
System.out.println("현재 스레드 이름(main): " + Thread.currentThread().getName());
46+
```
47+
2. Thread 객체 내부에서 스레드 정보 가져오기
48+
```java
49+
Thread thread1 = new Thread() {
50+
@Override
51+
public void run() {
52+
System.out.println("현재 스레드 (Thread 객체 사용): " + this);
53+
// 현재 스레드의 이름을 출력
54+
System.out.println("현재 스레드 이름 (Thread 객체 사용): " + getName());
55+
}
56+
};
57+
```
58+
- 위와 같은 경우는, 해당 스레드 내부에서 이름을 가져오는 것이기 때문에 바로 this 자체가 현재 스레드이다
59+
3. Runnable 내부에서 스레드 정보 가져오기
60+
```java
61+
Thread thread2 = new Thread(new ThreadName());
62+
63+
static class ThreadName implements Runnable {
64+
65+
@Override
66+
public void run() {
67+
System.out.println("현재 스레드 (Runnable 사용): " + Thread.currentThread());
68+
System.out.println("현재 스레드 이름 (Runnable 사용): " + Thread.currentThread().getName());
69+
}
70+
}
71+
```
72+
- Runnable은 여러 스레드 간 사용 가능하기 때문에 반드시 Thread.currentThread()를 지칭해서 현재 CPU를 할당받은 스레드 정보를 가져오도록 참조해야한다

src/main/java/io/concurrency/chapter03/exam04/ThreadNamingExample.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class ThreadNamingExample {
44
public static void main(String[] args) throws InterruptedException {
55

6+
67
// 스레드 이름을 생성자에 전달하여 설정
78
Thread myThread = new Thread(() -> {
89
System.out.println("현재 스레드 이름: " + Thread.currentThread().getName());
@@ -12,10 +13,11 @@ public static void main(String[] args) throws InterruptedException {
1213
// setName() 메서드를 사용하여 스레드 이름 설정
1314
Thread yourThread = new Thread(() -> {
1415
System.out.println("현재 스레드 이름: " + Thread.currentThread().getName());
15-
},"yourThread");
16-
// yourThread.setName("yourThread");
16+
});
17+
yourThread.setName("yourThread");
1718
yourThread.start();
1819

20+
1921
// 여러 개의 스레드를 생성하여 기본 스레드 이름을 출력
2022
for (int i = 0; i < 5; i++) {
2123
Thread defaultThread = new Thread(() -> {

0 commit comments

Comments
 (0)