700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java sigar cpu使用率_sigar相关-单个进程占CPU百分比

java sigar cpu使用率_sigar相关-单个进程占CPU百分比

时间:2022-07-11 07:24:15

相关推荐

java sigar cpu使用率_sigar相关-单个进程占CPU百分比

想得到针对每一个进程的占CPU的百分比,但是用getpercent()这个打印出来老是0.0。上网搜了好久也没有搜到用java写的得到单个进程占CPU百分比的例子。第一次接触这些东西所以懂得不多,求帮助T_T……最后再次谢谢!还有如果有数据了值也对不上。sleep有加,但也是没多大效果。

Sigarsigar=newSigar();

longpid=sigar.getPid();

intsleep_time=100;

SigarProxysigars=SigarProxyCache.newInstance(sigar,sleep_time);

ProcCpucpuu;

try{

ProcCpucpu=sigars.getProcCpu(pid);

cpu.getPercent();//itreturn0.0first

Thread.sleep(sleep_time);

cpuu=sigar.getProcCpu(pid);

System.out.println(cpuu.getPercent());

}catch(Exceptione){

e.printStackTrace();

}

20分

publicstaticvoidmain(String[]args)throwsSigarException{

finalRandomrand=newRandom();

newThread(){

publicvoidrun(){

while(true){

if(rand.nextBoolean()){

for(inti=0,l=10000000;i

rand.nextDouble();

}

}else{

try{

Thread.sleep(1000L);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

}.start();

NumberFormatformat=newDecimalFormat("0");

Sigarsigar=newSigar();

longpid=sigar.getPid();//javaw的pid或者注释掉上边的Thread,在资源管理器看pid

intavailableProcessors=Runtime.getRuntime().availableProcessors();

while(true){

System.out.println(format.format(sigar.getProcCpu(pid).getPercent()/availableProcessors*100)+""%"");

try{

Thread.sleep(1000L);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

引用1楼chenjing0320的回复:

publicstaticvoidmain(String[]args)throwsSigarException{

finalRandomrand=newRandom();

newThread(){

publicvoidrun(){

while(true){

if(rand.nextBoolean()){

for(inti=0,l=10000000;i

rand.nextDouble();

}

}else{

try{

Thread.sleep(1000L);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

}.start();

NumberFormatformat=newDecimalFormat("0");

Sigarsigar=newSigar();

longpid=sigar.getPid();//javaw的pid或者注释掉上边的Thread,在资源管理器看pid

intavailableProcessors=Runtime.getRuntime().availableProcessors();

while(true){

System.out.println(format.format(sigar.getProcCpu(pid).getPercent()/availableProcessors*100)+""%"");

try{

Thread.sleep(1000L);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

你给的方案可行。但是有一个问题请教,就是里面的数据貌似有时候不稳定,有时候CPU使用率会超过100%,比如1000%,请问这是什么原因的导致的?

引用2楼lewis121的回复:

Quote: 引用1楼chenjing0320的回复:

publicstaticvoidmain(String[]args)throwsSigarException{

finalRandomrand=newRandom();

newThread(){

publicvoidrun(){

while(true){

if(rand.nextBoolean()){

for(inti=0,l=10000000;i

rand.nextDouble();

}

}else{

try{

Thread.sleep(1000L);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

}.start();

NumberFormatformat=newDecimalFormat("0");

Sigarsigar=newSigar();

longpid=sigar.getPid();//javaw的pid或者注释掉上边的Thread,在资源管理器看pid

intavailableProcessors=Runtime.getRuntime().availableProcessors();

while(true){

System.out.println(format.format(sigar.getProcCpu(pid).getPercent()/availableProcessors*100)+""%"");

try{

Thread.sleep(1000L);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

你给的方案可行。但是有一个问题请教,就是里面的数据貌似有时候不稳定,有时候CPU使用率会超过100%,比如1000%,请问这是什么原因的导致的?

1000%没遇到过啊,我测的最多就100%

引用3楼chenjing0320的回复:

Quote: 引用2楼lewis121的回复:

Quote: 引用1楼chenjing0320的回复:

publicstaticvoidmain(String[]args)throwsSigarException{

finalRandomrand=newRandom();

newThread(){

publicvoidrun(){

while(true){

if(rand.nextBoolean()){

for(inti=0,l=10000000;i

rand.nextDouble();

}

}else{

try{

Thread.sleep(1000L);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

}.start();

NumberFormatformat=newDecimalFormat("0");

Sigarsigar=newSigar();

longpid=sigar.getPid();//javaw的pid或者注释掉上边的Thread,在资源管理器看pid

intavailableProcessors=Runtime.getRuntime().availableProcessors();

while(true){

System.out.println(format.format(sigar.getProcCpu(pid).getPercent()/availableProcessors*100)+""%"");

System.out.println(format.format(sigar.getProcCpu(pid).getPercent()/availableProcessors*100)+""%"");//该数据无效,都是0

try{

Thread.sleep(1000L);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

你给的方案可行。但是有一个问题请教,就是里面的数据貌似有时候不稳定,有时候CPU使用率会超过100%,比如1000%,请问这是什么原因的导致的?

1000%没遇到过啊,我测的最多就100%

还有一个问题噢,sigar.getProcCpu(pid).getPercent();如果再输出一遍。貌似数据不对,几乎都是0。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。