Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-10-2008, 12:37 PM
Member
 
Join Date: Dec 2007
Posts: 19
JavaEmpires is on a distinguished road
How to determine CPU usage using Java?????
Hi all,

I want to do a CPU monitoring program and for that I need the CPU usage per second.
How do i determine the CPU usage using Java ???
Please give any suggestions as to how to go about this???
OS i am using is Linux.

Thanks in advance
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-10-2008, 12:43 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 251
roots is on a distinguished road
Profiling CPU usage from within a Java application - Java World

google:java cpu usage
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-11-2008, 12:19 AM
Member
 
Join Date: Jan 2008
Posts: 1
Deathbob is on a distinguished road
There is a way using pure java. It may take some adapting to fit your needs. The monitoring could be placed on it's own thread and have a handle to the thread you wish to monitor.

cpuperc in this case holds the aprox. cpu usage %. It is fairly accurate. You could average several to get a better idea.

Code:
//import java.lang.management.*; ThreadMXBean TMB = ManagementFactory.getThreadMXBean(); long time = new Date().getTime() * 1000000; long cput = 0; double cpuperc = -1; //Begin loop. if( TMB.isThreadCpuTimeSupported() ) { if(new Date().getTime() * 1000000 - time > 1000000000) //Reset once per second { time = new Date().getTime() * 1000000; cput = TMB.getCurrentThreadCpuTime(); } if(!TMB.isThreadCpuTimeEnabled()) { TMB.setThreadCpuTimeEnabled(true); } if(new Date().getTime() * 1000000 - time != 0) cpuperc = (TMB.getCurrentThreadCpuTime() - cput) / (new Date().getTime() * 1000000.0 - time) * 100.0; } else { cpuperc = -2; } //Do cpu intensive stuff //End Loop
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Timer usage in java program sandeeprao.techno New To Java 5 01-24-2008 08:16 PM
Integer Usage bluegreen7hi New To Java 11 01-24-2008 09:54 AM
How to determine all the IP addresses of machines on local LAN?? JavaEmpires Networking 3 01-19-2008 07:05 AM
how to determine the primary key osval Database 1 08-07-2007 03:31 AM
How to determine if the value of a JTextField is double nemo New To Java 1 05-20-2007 12:19 PM


All times are GMT +3. The time now is 12:01 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org