멀티쓰레드 프로그래밍

메인쓰레드

public static void main(String[] args) {
	Thread mainThread = Thread.currentThread(); 	        
  System.out.println(mainThread.getName());
}

public static void main(String[] args) 
  {
     try 
     {
    Thread.currentThread().join();
        // the following statement will never execute because main thread join itself 
        System.out.println("This statement will never execute"); 
     }
     catch (InterruptedException e) 
     {
    e.printStackTrace();
     }
  }

데몬쓰레드

https://javagoal.com/main-thread-in-java/

https://javagoal.com/main-thread-in-java/

위 사진에서도 볼 수 있듯이, 메인 쓰레드 옆에 데몬쓰레드라는것도 존재합니다.

데몬 쓰레드들은 보조적인 역할로 사용되는 쓰레드들 입니다 (GC, 모니터링 등..). 일반 쓰레드가 종료되면 데몬 쓰레드는 강제적으로 종료되는 특징이 있기 때문에, 지속적인 관찰이 필요한 어플리케이션에서 유용하게 사용할 수 있습니다. 메인 메소드 종료시에 무한 루프도 종료되기 때문입니다.

아래는 데몬 쓰레드를 설정할 수 있는 예제입니다.

public static void main(String[] args) {
	Thread th = new ThreadExample();
	th.setDaemon(true); // 데몬쓰레드들로 만들기.
	th.start();
}