Java/12. 멀티 스레드 (Multi Thread)

Day 17 : 스레드 그룹 이름 얻기

pancakemaker 2021. 11. 10. 18:14

스레드 그룹(ThreadGroup) : 관련된 스레드를 묶어서 관리할 목적으로 이용

 

system 스레드 그룹 : JVM 운영에 필요한 스레드 포함

system/main 그룹 : 메인 스레드 포함

 

스레드는 반드시 하나의 스레드 그룹에 포함

명시적으로 포함시키지 않으면 기본적으로 자신을 생성한 스레드와 같은 스레드 그룹에 속함

 

스레드 그룹 이름 얻기 :

ThreadGroup group = Thread.currentThread().getThreadGroup();

String groupName = group.getName();

 

② Thread의 정적 메소드 getAllStackTraces() 이용

Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();

 

현재 실행 중인 스레드 정보

package sec08.exam01_threadgroup_YJ;

import java.util.Map;
import java.util.Set;

import sec07.exam01_daemon_YJ.AutoSaveThread;

public class ThreadInfoExample {
	public static void main(String[] args) {
		AutoSaveThread autoSaveThread = new AutoSaveThread();
		autoSaveThread.setName("AutoSaveThread");
		autoSaveThread.setDaemon(true);
		autoSaveThread.start();
		
		Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
		Set<Thread> threads = map.keySet();
		for(Thread thread : threads) { //Thread를 하나씩 가져와 루핑시킴
			System.out.println("Name: " + thread.getName() + ((thread.isDaemon())?"(데몬)":"(주)"));
			System.out.println("\t" + "소속그룹: " + thread.getThreadGroup().getName());
			System.out.println();
		}
	}
}
Name: Finalizer(데몬)
	소속그룹: system

Name: Attach Listener(데몬)
	소속그룹: system

Name: Signal Dispatcher(데몬)
	소속그룹: system

Name: Reference Handler(데몬)
	소속그룹: system

Name: main(주)
	소속그룹: main

Name: AutoSaveThread(데몬)
	소속그룹: main