Java implementation compute process CPU usage

Source: Internet
Author: User
Tags cpu usage

Code Sample:

MAVEN Package

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.zetyun.mml</groupId> <artifactId> My-mml</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> & lt;name>my-mml</name> <url>http://maven.apache.org</url> <properties> <projec T.build.sourceencoding>utf-8</project.build.sourceencoding> <maven.compiler.source>1.8</maven  
  
  
  .compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <build> <defaultGoal>compile</defaultGoal> <plugins> ;p lugin> <artifactid>maven-assembly-Plugin</artifactid> <configuration> <archive> </archive> <descriptorRefs> <descriptorref>jar-with-  
                Dependencies</descriptorref> </descriptorRefs> </configuration> <executions> <execution> <id>make-asse  
                            Mbly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> &L t;/execution> </executions> </plugin> </plugins> < /build> <repositories><!--code base--> <repository> <id>maven-  
       Ali</id>     <url>http://maven.aliyun.com/nexus/content/groups/public</url> <releases>  
                <enabled>true</enabled> </releases> <snapshots>  
                <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> &L t;/repositories> <dependencies> <dependency> <groupId>junit</groupId> <
    Artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.danielflower.apprunner</groupId> <ar tifactid>javasysmon</artifactid> <version>0.3.5.0</version> </dependency> </ Dependencies> &LT;/PROject>
 

Package com.zetyun.mml.base;
Import Com.jezhumble.javasysmon.CpuTimes;
Import Com.jezhumble.javasysmon.JavaSysMon;

Import Com.jezhumble.javasysmon.ProcessInfo;
		public class Javasysmonutils {public static void main (string[] args) {String pid;
		String period;

		Javasysmon monitor = new Javasysmon ();
			if (args.length!= 2) {pid = string.valueof (Monitor.currentpid ());
		Period = "5000";
			else {pid = args[0];
		period = args[1];
			} while (true) {String processusage = String.Format ("%.2f", Getprocessusage (monitor, PID, period) * 100);
			System.out.println (Processusage);
		System.out.println ("------------------------------------");
	}//Getprocsscpuuage (); private static float getprocessusage (Javasysmon Monitor, string pid, String period) {Long Cputotaltimebegin = getc
		Putotaltime (monitor);

		Long Processtotaltimebegin = getprocesstotaltimebypid (monitor, Integer.parseint (PID));
		try {thread.sleep (Long.parselong (period)); catch (InterruPtedexception e) {//TODO auto-generated catch block E.printstacktrace ();
		Long cputotaltimeend = getcputotaltime (monitor);

		Long processtotaltimeend = getprocesstotaltimebypid (monitor, Integer.parseint (PID)); float processusage = (float) (processtotaltimeend-processtotaltimebegin)/(float) (Cputotaltimeend-cputotaltimebe

		gin);

	return processusage;
	private static long Getcputotaltime (Javasysmon monitor) {return monitor.cputimes (). Gettotalmillis (); private static long Getprocesstotaltimebypid (Javasysmon monitor, int pid) {ProcessInfo ProcessInfo = Findprocessby

		PID (monitor, PID);
		if (ProcessInfo = = null) {return 0;

	return Processinfo.getsystemmillis () + Processinfo.getusermillis (); public static ProcessInfo Findprocessbypid (Javasysmon monitor, int pid) {processinfo[] Processinfos = Monitor.proc

		Esstable ();
	for (ProcessInfo Processinfo:processinfos) {if (PID = = Processinfo.getpid ()) {return processinfo;		} return null;

		public static void Getcpuusge () {Javasysmon monitor = new Javasysmon ();

			while (true) {Cputimes cputimes = Monitor.cputimes ();

			System.out.println ("\ n--------------------");
			try {thread.sleep (5000);
			catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();

			} cputimes CpuTimes2 = Monitor.cputimes ();

			String CPUUsage = String.Format ("%.5f", Cputimes2.getcpuusage (Cputimes));

		System.out.println ("CPUUsage:" + cpuusage);
 }

	}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.