데몬 스레드는 우선순위가 낮은 스레드입니다. 백그라운드에서 실행되고 대부분 가비지 컬렉션(GC)과 같은 백그라운드 작업을 수행하기 위해 JVM에 의해 생성되는 자바에서. 실행 중인 사용자 스레드가 없으면 데몬 스레드가 실행 중이더라도 JVM이 종료될 수 있습니다. 데몬 스레드의 유일한 목적은 사용자 스레드를 제공하는 것입니다. isDaemon() 메소드를 사용하여 스레드가 데몬 스레드인지인지 확인할 수 있습니다.
구문
Public boolean isDaemon()
예시
class SampleThread implements Runnable {
public void run() {
if(Thread.currentThread().isDaemon())
System.out.println(Thread.currentThread().getName()+" is daemon thread");
else
System.out.println(Thread.currentThread().getName()+" is user thread");
}
}
// Main class
public class DaemonThreadTest {
public static void main(String[] args){
SampleThread st = new SampleThread();
Thread th1 = new Thread(st,"Thread 1");
Thread th2 = new Thread(st,"Thread 2");
th2.setDaemon(true); // set the thread th2 to daemon.
th1.start();
th2.start();
}
} 출력
Thread 1 is user thread Thread 2 is daemon thread