-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava208_thread.java
More file actions
52 lines (39 loc) · 1.22 KB
/
Copy pathJava208_thread.java
File metadata and controls
52 lines (39 loc) · 1.22 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package java0914_thread;
/*
* 스레드 생명주기(Life Cycle)
* start() - 실행준비상태(RUNNABLE) - run() - TERMINATED - 대기상태(WAITING, NOT RUNNALBLE)
*/
class LifeCycle extends Thread {
public LifeCycle() {
}
@Override
public void run() {
System.out.println(getState()); // RUNNABLE
for (int i = 0; i <= 5; i++) {
System.out.printf("%s i=%d\n", Thread.currentThread().getName(), i);
try {
// 1000은 1초를 의미한다.(millisecond)
Thread.sleep(1000); // WAITING(NOT RUNNABLE) - 일시정지상태
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Java208_thread {
public static void main(String[] args) {
LifeCycle cc = new LifeCycle();
// getState() : 현재 사용하는 스레드의 상태를 확인한다.
System.out.println(cc.getState()); // NEW
cc.start();
try {
// 지정된 시간동안 스레드가 실행되도록 한다.
// 지정된 시간이 지나거나 종료가 되면 join()을 호출한 스레드로 다시 돌아와 실행을 계속 수행한다.
cc.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(cc.getState()); // TERMINATED
System.out.println("main end");
}
}