Find out where a thread has been blocked
In java, i have 2 threads started at the same time. However, i noticed the thread 2 is being blocked but i do not know where. Is there a way in java to find out where a thread has been开发者_如何学Python blocked/waiting? thank u.
You could use jstack
to get the stack traces for all threads. That'll tell you where exactly in the code your thread is blocked.
To get a more detailed picture of what's happening inside your process, you could use VisualVM
.
You can use the isAlive()
method to verify if the thread is still active or not. Here's a sample code to check all the threads in your memory:
public class ThreadStates{
public static void main(String[] args){
Thread t = new Thread();
Thread.State e = t.getState();
Thread.State[] ts = e.values();
for(int i = 0; i < ts.length; i++){
System.out.println(ts[i]);
}
}
}
You can use jstack
to find where a thread is blocked. However if you want to check a thread within the same program you can using thread.getStackTrace()
精彩评论