JPDA 架构研究6 - Agent利用环境指针访问VM (线程管理篇)

引入:数组

上篇文章讲解了分类:内存管理,如今讲线程类操做的接口。
jvm


分类2:线程类操做ide

a.GetThreadState.获取线程状态线程

jvmtiError
GetThreadState(jvmtiEnv* env,
            jthread thread,
            jint* thread_state_ptr)

你们都知道线程有不少种状态,好比Alive,Terminated,Runnable, 等待进入Synchronize Block,Waiting,Sleeping,Parked,Suspended,Interrupted等。对象

它入参thread表示要查询的线程,返回thread_state_ptr表示线程状态。
接口


b.GetAllThreads.获取全部活着的线程,这些线程必须是链接到当前VM而且Alive的。
内存

jvmtiError
GetAllThreads(jvmtiEnv* env,
            jint* threads_count_ptr,
            jthread** threads_ptr)

它返回threads_count_ptr表示活着的线程数量,返回threads_ptr表示活着的线程的引用数组。it


c.SuspendThread.挂起某线程内存管理

jvmtiError
SuspendThread(jvmtiEnv* env,
            jthread thread)

一旦挂起某线程,则对应方法没法返回直到另外有某个线程调用ResumeThread.io

它入参thread表示要挂起的线程。


d.SuspendThreadList.挂起一组线程

jvmtiError
SuspendThreadList(jvmtiEnv* env,
            jint request_count,
            const jthread* request_list,
            jvmtiError* results)

一旦在ThreadList中某线程被挂起,则只有其余某线程调用ResumeThreadList或者ResumeThread后,此线程对应方法才能够返回。

入参request_count表示要挂起的线程数量,request_list,表示要挂起的线程数组,返回results表示挂起结果。


e.ResumeThread.恢复某个被挂起的线程

jvmtiError
ResumeThread(jvmtiEnv* env,
            jthread thread)


f.ResumeThreadList.恢复某个被挂起的线程组

jvmtiError
ResumeThreadList(jvmtiEnv* env,
            jint request_count,
            const jthread* request_list,
            jvmtiError* results)


g.StopThread.杀死某线程

jvmtiError
StopThread(jvmtiEnv* env,
            jthread thread,
            jobject exception)


h.InterruptThread.中断某线程

vmtiError
InterruptThread(jvmtiEnv* env,
            jthread thread)


i.GetThreadInfo.获取某线程信息

typedef struct {
    char* name;
    jint。 priority;
    jboolean is_daemon;
    jthreadGroup thread_group;
    jobject context_class_loader;
} jvmtiThreadInfo;
jvmtiError
GetThreadInfo(jvmtiEnv* env,
            jthread thread,
            jvmtiThreadInfo* info_ptr)

从这里能够看出,这里能够获取线程的名字,优先级,是否守护线程,所属线程组,上下文加载器等信息。


j.GetOwnerMonitorInfo.获取线程拥有的监视器(能够多个)信息

jvmtiError
GetOwnedMonitorInfo(jvmtiEnv* env,
            jthread thread,
            jint* owned_monitor_count_ptr,
            jobject** owned_monitors_ptr)

我在这里对Monitor的理解是线程持有的锁,也就是synchronized所关联的对象。

入参还是给定线程,返回监视器的数量和监视器引用的数组。


k.GetCurrentContendedMonitor.获取线程当前的监视器。

jvmtiError
GetCurrentContendedMonitor(jvmtiEnv* env,
            jthread thread,
            jobject* monitor_ptr)

和上面配套使用。由于多个竞争者同时共享某线程,那么确定有某个当前竞争者占用了此线程的执行。


l.SetThreadLocalStorage.VM设置一个thread-local的值来关联到某 环境-线程对。

jvmtiError
SetThreadLocalStorage(jvmtiEnv* env,
            jthread thread,
            const void* data)


m.GetThreadLocalStorage.Agent来获取为某线程设置的thread-local值。

jvmtiError
GetThreadLocalStorage(jvmtiEnv* env,
            jthread thread,
            void** data_ptr)
相关文章
相关标签/搜索