-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadNameExample.java
More file actions
29 lines (27 loc) ยท 1.01 KB
/
ThreadNameExample.java
File metadata and controls
29 lines (27 loc) ยท 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package ch14.sec04;
public class ThreadNameExample {
public static void main(String[] args) {
// ์ฝ๋๋ฅผ ์คํํ๋ ์ค๋ ๋ ๊ฐ์ฒด ์ฐธ์กฐ ์ป๊ธฐ
Thread mainThread = Thread.currentThread();
System.out.println(mainThread.getName() + " ์คํ"); // getName : main
for (int i = 0; i < 3; i++) {
Thread t1 = new Thread() {
@Override
public void run() {
// getName() method ๋ Thread instance method ๋ก thread name return
System.out.println(getName() + " ์คํ"); // getName : Thread-0,1,...
}
};
t1.start();
}
Thread chatThread = new Thread() {
@Override
public void run() {
System.out.println(getName() + " ์คํ");
}
};
// ๊ทธ๋ฅ ์คํํ๋ฉด Thread-3 ์ด๋ผ setName method ๋ก name ๋ถ์ฌํ์ฌ ๋ณ๊ฒฝ
chatThread.setName("chat-thread");
chatThread.start();
}
}