>  기사  >  Java  >  Java에서 isDaemon() 메소드의 중요성은 무엇입니까?

Java에서 isDaemon() 메소드의 중요성은 무엇입니까?

WBOY
WBOY앞으로
2023-08-24 15:29:031058검색

Java에서 isDaemon() 메소드의 중요성은 무엇입니까?

데몬 스레드는 백그라운드에서 실행되는 Java의 우선 순위가 낮은 스레드이며 일반적으로 Garbage Collection(GC)과 같은 백그라운드 작업을 수행하기 위해 JVM에서 생성됩니다. 실행 중인 사용자 스레드가 없으면 데몬 스레드가 실행 중이더라도 JVM이 종료될 수 있습니다. 데몬 스레드의 유일한 목적은 사용자 스레드에 서비스를 제공하는 것입니다. isDaemon() 메서드를 사용하여 스레드가 daemon 스레드인지 확인할 수 있습니다.

Grammar

Public boolean isDaemon()

Example

의 중국어 번역은 다음과 같습니다.

Example

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();
   }
}

Output

Thread 1 is user thread
Thread 2 is daemon thread

위 내용은 Java에서 isDaemon() 메소드의 중요성은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제