Implementing a JVMTI agent, I read the threads state on certain events, for instance ThreadStart, ThreadEnd, VMInit, VMDeath, etc. However, I noticed, that the threads state is never new or terminated, but always runnable, wait, etc.
There might be a reason, ThreadEnd is just a signal that the thread is terminating. I read all Threads with GetAllThreads
which says:
Get all live threads. The threads are Java programming language threads; that is, threads that are attached to the VM. A thread is live if java.lang.Thread.isAlive() would return true, that is, the thread has been started and has not yet died. The universe of threads is determined by the context of the JVM TI environment, which typically is all threads attached to the VM. Note that this includes JVM TI agent threads (see RunAgentThread).
Which means, even if I call this at a VMDeath event, I won't fetch the terminated threads anyway. But am I supposed to do in order to get the states terminated and new?
My guess:
- on the event ThreadEnd, I return the state terminated manually
- on object allocation, if it's a thread, I return the state new manually
P.S.: I am reading the threads state as suggested by the JVMTI API
err = (*jvmti)->GetThreadState(jvmti, thread, &state);
abortOnError(err);
switch (state & JVMTI_JAVA_LANG_THREAD_STAT开发者_JAVA百科E_MASK) {
case JVMTI_JAVA_LANG_THREAD_STATE_NEW:
return "NEW";
case JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED:
return "TERMINATED";
case JVMTI_JAVA_LANG_THREAD_STATE_RUNNABLE:
return "RUNNABLE";
case JVMTI_JAVA_LANG_THREAD_STATE_BLOCKED:
return "BLOCKED";
case JVMTI_JAVA_LANG_THREAD_STATE_WAITING:
return "WAITING";
case JVMTI_JAVA_LANG_THREAD_STATE_TIMED_WAITING:
return "TIMED_WAITING";
}
精彩评论