-
Java Virtual Thread (7), Virtual Thread Dump프로그래밍/Java 2024. 6. 23. 15:04
기본적으로 인텔리제이에서는 Thread Dump와 비슷한 기능을 제공하고 있다.
현재 인텔리제이에서는 디버그 모드를 통해 Virtual Thread가 몇개 떠 있는지 정확하게 알 수 없다. (2024.01 버전 기준)
@Slf4j // jcmd <PID> Thread.dump_to_file -format=text <file> public class VirtualThreadDump { private static final Runnable runnable = new Runnable() { @Override public void run() { log.info("1) run. thread: " + Thread.currentThread()); try { Thread.sleep(30_000); } catch (InterruptedException e) { throw new RuntimeException(e); } log.info("2) run. thread: " + Thread.currentThread()); } }; public static void main(String[] args) throws InterruptedException { log.info("1) main. thread: " + Thread.currentThread()); taskExecutor(); log.info("2) main. thread: " + Thread.currentThread()); } private static void taskExecutor() { ThreadFactory factory = Thread.ofVirtual().name("myVirtual-", 0).factory(); try (ExecutorService executorService = Executors.newThreadPerTaskExecutor(factory)) { for (int i = 0; i < 33; i++) { executorService.submit(runnable); } } } }
virtual thread의 thread dump 확인을 위해 33개의 virtual thread를 생성하고 sleep을 30초 설정해보자.
Thread Dump를 확인하기 위해 cmd > jstack 혹은 jcmd 를 통해 확인해야 한다.
1) jps 입력
2) jps 에 해당하는 pid 확인
3) 아래와 같이 jcmd 입력
// jcmd <PID> Thread.dump_to_file -format=text <file> (ex. dump.dump)
그러면 dump.dump 파일이 workspace에 생성된 것을 확인할 수 있다.
그리고 파일을 확인해보면 sleep 이 어디서 일어났는지 등의 call stack을 확인할 수 있다.
'프로그래밍 > Java' 카테고리의 다른 글
Java Virtual Thread (10), @async 어노테이션 (0) 2024.06.29 Java Virtual Thread (8), Virtual Thread와 Platform Thread 성능비교 (0) 2024.06.23 Java Virtual Thread (5), Pinned Virtual Thread (0) 2024.06.23 Java Virtual Thread (3), executorService 생성 (0) 2024.06.22 Java Virtual Thread (2), Thread와 Virtual Thread (0) 2024.06.22