quilt code
[고급자바] Thread(3) 본문
1. 데몬 스레드 예제
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class T09ThreadDaemonTest {
public static void main(String[] args) {
Thread th = new AutoSaveThread();
// 데몬스레드로 설정하기(start()메소드를 호출하기 전에 설정해야 한다.
th.setDaemon(true);
th.start();
try {
for(int i=1; i<=20; i++) {
System.out.println("작업 " + i);
Thread.sleep(1000);
}
} catch(InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("메인 스레드 종료...");
}
}
|
cs |
2. 자동 저장하는 기능을 제공하는 스레드 (3초에 한번씩 저장하기)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class AutoSaveThread extends Thread {
public void save() {
System.out.println("작업 내용을 저장합니다...");
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
save(); // 저장기능 호출
}
}
}
|
cs |
3. 스레드의 상태
| NEW | 스레드가 생성되고 아직 start()가 호출되지 않은 상태 |
| RUNNABLE | 실행 중 또는 실행 가능한 상태 |
| BLOCKED | 동기화 블럭에 의해서 일시정지된 상태(lock이 풀릴 때까지 기다리는 상태) |
| WAITING, TIMED_WAITING | 스레드의 작업이 종료되지는 않았지만 실행가능하지 않은 일시정지 상태 TIMED_WAITING은 일시정지 시간이 지정된 경우임. |
| TERMINATED | 스레드의 작업이 종료된 상태 |
|
1
2
3
4
5
6
7
|
public static void main(String[] args) {
Thread th = new StatePrintThread(new TargetThread());
th.start();
}
|
cs |
1) 상태 변화를 지켜볼 대상 스레드
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class TargetThread extends Thread {
@Override
public void run() {
for(int i=1; i<=1000000000; i++) { // 시간 지연용
}
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=1; i<=1000000000; i++) { // 시간 지연용
}
}
}
|
cs |
2) 대상 스레드의 상태변화를 출력하기 위한 스레드
|
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
|
class StatePrintThread extends Thread {
private Thread targetThread;
public StatePrintThread(Thread targetThread) {
this.targetThread = targetThread;
}
@Override
public void run() {
while(true) {
// Thread의 상태 구하기
Thread.State state = targetThread.getState();
System.out.println("대상 스레드의 상태값 : " + state);
// NEW 상태인지 검사
if(state == Thread.State.NEW) {
targetThread.start();
}
// 타겟 스레드가 종료 상태인지 검사
if(state == Thread.State.TERMINATED) {
break;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
|
cs |
4. DisplayCharacter
** 3개(명)의 스레드가 각각 알파벳 대문자를 출력하는데 출력을 끝낸 순서대로 결과를 나타내는 프로그램을 작성하시오.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
static int currRank = 1; // 현재 순위 정보
public static void main(String[] args) {
List<DisplayCharacter> disCharList = new ArrayList<>();
disCharList.add(new DisplayCharacter("정재현"));
disCharList.add(new DisplayCharacter("이태민"));
disCharList.add(new DisplayCharacter("김준면"));
disCharList.add(new DisplayCharacter("변백현"));
for(Thread th : disCharList) {
th.start();
}
for(Thread th : disCharList) {
try {
th.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("경기 끝...");
}
|
cs |
1) 알파벳 대문자 출력을 하는 스레드 클래스
|
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
|
class DisplayCharacter extends Thread {
private String name;
private int rank;
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public DisplayCharacter(String name) {
super(name);
this.name = name;
}
@Override
public void run() {
for(char ch='A'; ch<='Z'; ch++) {
System.out.println(name + "의 출력 문자 : " + ch);
try {
// 200 ~ 500 ms 사이의 정지상태가 일어남
Thread.sleep((int)(Math.random()*301 + 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + "출력 끝...");
setRank(T11DisplayCharacterTest.currRank++); // 현재 순위 정보 설정
}
}
|
cs |
'daily > 고급자바' 카테고리의 다른 글
| [고급자바] Thread (4) (0) | 2023.02.10 |
|---|---|
| [고급자바] 컴퓨터와 가위바위보 프로그램 (0) | 2023.02.07 |
| [고급자바] Thread (2) (0) | 2023.02.07 |
| [고급자바] Thread (1) (0) | 2023.02.06 |
| [고급자바] Annotation (0) | 2023.02.06 |