oshi方法获取cpu、内存和系统当前时间瞬时信息

oshi方法获取cpu、内存和系统当前时间瞬时信息!

pom.xml

 <!-- oshi方法获取cpu -->
       <dependency>
            <groupId>com.github.oshi</groupId>
            <artifactId>oshi-core</artifactId>
            <version>3.5.0</version>
       </dependency>


核心代码:

package com.sunwave.domain;

import java.text.DecimalFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;


@SuppressWarnings("restriction")
@Slf4j
public class test {
	//日志打印	
	private final static Logger logger = LoggerFactory.getLogger(test.class);
    	
	
//oshi方法获取cpu信息		
public static void main(String[] args) throws Exception {
	//1、获取cpu信息
		while(true) {
	        SystemInfo systemInfo = new SystemInfo();
	        CentralProcessor processor = systemInfo.getHardware().getProcessor();
	        long[] prevTicks = processor.getSystemCpuLoadTicks();
	        long[] ticks = processor.getSystemCpuLoadTicks();
	        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
	        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
	        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
	        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
	        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
	        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
	        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()]; 
	        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
	        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
	        
	        logger.info("CPU总数 = {},CPU利用率 ={}",processor.getLogicalProcessorCount(),new DecimalFormat("#.##%").format(1.0-(idle * 1.0 / totalCpu)));
	        
	        Thread.sleep(2000);
	      
	       System.out.println("系统开始时间:"+System.currentTimeMillis());
	       
	       System.out.println("cpu利用率:"+(1.0-(idle * 1.0 / totalCpu)));
	       
	   //2、获取当前系统时间
	        long startTime =  System.currentTimeMillis();//初始时间
	        //获取当前的系统时间,与初始时间相减就是程序运行的毫秒数,除以1000就是秒数
			long endTime =  System.currentTimeMillis();
			long usedTime = (endTime-startTime)/1000;
	
	        
	  //3、获取内存信息    
		        GlobalMemory memory = systemInfo.getHardware().getMemory();
		        long totalByte = memory.getTotal();
		        long acaliableByte = memory.getAvailable();
				
		        logger.info("可用内存大小 = {},内存使用率 ={}",formatByte(totalByte),new DecimalFormat("#.##%").format((totalByte-acaliableByte)*1.0/totalByte));
		        Thread.sleep(1000);
		}
	
		
	}
     //3、获取内存信息调用
  public static String formatByte(long bNumber){
    double formatB = 1024.0;
    double kNumber = bNumber/formatB;
    if(kNumber<formatB){
        return new DecimalFormat("#.##KB").format(kNumber);
    }
    double mbNumber = kNumber/formatB;
    if(mbNumber<formatB){
        return new DecimalFormat("#.##MB").format(mbNumber);
    }
    double gbNumber = mbNumber/formatB;
    if(gbNumber<formatB){
        return new DecimalFormat("#.##GB").format(gbNumber);
    }
    double tbNumber = gbNumber/formatB;
    return new DecimalFormat("#.##TB").format(tbNumber);
}

}

大功告成!
亲测有效!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值