简体   繁体   English

计算线程被抢占了多少次?

[英]Counting how many times a thread has been preempted?

I am trying to determine the impact of context switching on my Java thread. 我试图确定上下文切换对我的Java线程的影响。 Therefore I would have to know first "how many times has it been preempted?" 因此,我必须首先知道“它被抢占了多少次?” and second, if possible, "how long was my thread suspended from execution?". 第二,如果可能的话,“我的线程被暂停执行了多长时间?”。 I would doubt very much that this can be done from Java, but hey! 我非常怀疑这可以从Java中完成,但是, 嘿! the fun things is to do the impossible, right? 有趣的事情是做不可能的事情,对不对? :) Even If I have to recompile the VM or to use some kind of LINUX tool or hack I would be happy. :)即使我必须重新编译VM或使用某种LINUX工具或黑客,我也会很高兴。 So now that we know that can't be done, let's do it. 因此,既然我们知道这不可能完成,那就开始吧。 :) :)

EDIT: Come on people. 编辑:加油。 The other question was priority related. 另一个问题与优先权有关。 I guess you just enjoy easy and boring questions. 我想您只是喜欢简单而无聊的问题。 The question is clear and valid. 这个问题是明确和有效的。

如果您可以为感兴趣的Java线程标识Linux TID(线程ID)(假设JVM将Java线程映射到Linux线程),则伪文件/proc/<tid>/status包含许多统计信息,包括voluntary_ctxt_switchesnonvoluntary_ctxt_switches

显然,它并不能满足您的所有需求,但是您可能会发现java.lang.management提供的ThreadMXBean很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM