zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java 7中新增的CPU和负载的监控详解编程语言

2023-06-13 09:20:45 时间
import java.lang.management.ManagementFactory; 

import java.lang.management.OperatingSystemMXBean; 

import java.lang.reflect.Method; 

 * Test 

public class Test { 

 public static void main(String[] args) throws InterruptedException { 

 OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); 

 while (true) { 

 double load; 

 try { 

 Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage"); 

 load = (Double)method.invoke(operatingSystemMXBean); 

 } catch (Throwable e) { 

 load = -1; 

 int cpu = operatingSystemMXBean.getAvailableProcessors(); 

 if (load = cpu) { 

 System.err.println("WARN!!load:" + load + ","+ "cpu:" + cpu); 

 Thread.currentThread().sleep(1 * 1000); 

}

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/15907.html

cjava