package threadEx;
/*
- 독립 스레드
메인은 프로그램의 시작점이지 종료점은 아니다.
메인 스레드가 종료되어도 독립 스레드의 작업이 완료될 때까지 프로그램은 종료되지 않는다.
- 데몬 스레드
다른 스레드에게 도움을 주기 위한 스레드
메인 스레드가 종료되면 데몬 스레기의 작업이 완료되지 않아도 프로그램은 종료됨.
*/
public class Ex006_Daemon {
public static void main(String[] args) {
MyThread6 t1 = new MyThread6();
MyThread6 t2 = new MyThread6();
MyThread6 t3 = new MyThread6();
// 스레드들 데몬으로 만듦
t1.setDaemon(true);
t2.setDaemon(true);
t3.setDaemon(true);
t1.start();
t2.start();
t3.start();
try {
t1.join(); // 스레드가 종료될 때 까지 대기
t2.join();
t3.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("메인 종료...");
}
}
class MyThread6 extends Thread {
@Override
public void run() {
for (int i = 1; i <= 20; i++) {
System.out.println(getName() + " -> " + i);
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
}
}