JPDA 架构研究6 - Agent利用环境指针访问VM (线程管理篇)
引入: 上篇文章讲解了分类:内存管理,现在讲线程类操作的接口。 分类2:线程类操作 a.GetThreadState.获取线程状态 jvmtiError GetThreadState(jvmtiEnv*env, jthreadthread, 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表示活着的线程的引用数组。 c.SuspendThread.挂起某线程 jvmtiError SuspendThread(jvmtiEnv*env, jthreadthread) 一旦挂起某线程,则对应方法无法返回直到另外有某个线程调用ResumeThread. 它入参thread表示要挂起的线程。 d.SuspendThreadList.挂起一组线程 jvmtiError SuspendThreadList(jvmtiEnv*env, jintrequest_count, constjthread*request_list, jvmtiError*results) 一旦在ThreadList中某线程被挂起,则只有其他某线程调用ResumeThreadList或者ResumeThread后,此线程对应方法才可以返回。 入参request_count表示要挂起的线程数量,request_list,表示要挂起的线程数组,返回results表示挂起结果。 e.ResumeThread.恢复某个被挂起的线程 jvmtiError ResumeThread(jvmtiEnv*env, jthreadthread) f.ResumeThreadList.恢复某个被挂起的线程组 jvmtiError ResumeThreadList(jvmtiEnv*env, jintrequest_count, constjthread*request_list, jvmtiError*results) g.StopThread.杀死某线程 jvmtiError StopThread(jvmtiEnv*env, jthreadthread, jobjectexception) h.InterruptThread.中断某线程 vmtiError InterruptThread(jvmtiEnv*env, jthreadthread) i.GetThreadInfo.获取某线程信息 typedefstruct{ char*name; jint。priority; jbooleanis_daemon; jthreadGroupthread_group; jobjectcontext_class_loader; }jvmtiThreadInfo; jvmtiError GetThreadInfo(jvmtiEnv*env, jthreadthread, jvmtiThreadInfo*info_ptr) 从这里可以看出,这里可以获取线程的名字,优先级,是否守护线程,所属线程组,上下文加载器等信息。 j.GetOwnerMonitorInfo.获取线程拥有的监视器(可以多个)信息 jvmtiError GetOwnedMonitorInfo(jvmtiEnv*env, jthreadthread, jint*owned_monitor_count_ptr, jobject**owned_monitors_ptr) 我在这里对Monitor的理解是线程持有的锁,也就是synchronized所关联的对象。 入参仍是给定线程,返回监视器的数量和监视器引用的数组。 k.GetCurrentContendedMonitor.获取线程当前的监视器。 jvmtiError GetCurrentContendedMonitor(jvmtiEnv*env, jthreadthread, jobject*monitor_ptr) 和上面配套使用。因为多个竞争者同时共享某线程,那么肯定有某个当前竞争者占用了此线程的执行。 l.SetThreadLocalStorage.VM设置一个thread-local的值来关联到某 环境-线程对。 jvmtiError SetThreadLocalStorage(jvmtiEnv*env, jthreadthread, constvoid*data) m.GetThreadLocalStorage.Agent来获取为某线程设置的thread-local值。 jvmtiError GetThreadLocalStorage(jvmtiEnv*env, jthreadthread, void**data_ptr) 本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/1587694,如需转载请自行联系原作者