Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to view where a thread is in code java

Tags:

java

debugging

Can you print out what line in the code and what method a running thread is in, from within a program - without using some monitoring software like visualvm? Do you have to extend Thread?

like image 715
Michael Roller Avatar asked Feb 20 '23 15:02

Michael Roller


1 Answers

If this is just for quick-and-dirty debugging or error reporting, you can just do

new Exception().printStackTrace()

If you want to be a little fancier than that, you can use Thread.currentThread().getStackTrace() to get an array of StackTraceElement objects; you can determine the current line number by examining those.

like image 184
Ernest Friedman-Hill Avatar answered Feb 27 '23 03:02

Ernest Friedman-Hill