• Post Reply Bookmark Topic Watch Topic
  • New Topic

100% CPU usage or bad code

 
Daniil Sosonkin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have this program that reads about 2MB of data per second from a socket and then parses it. The data consists of messages which look like this: f72,0f4o385000y390000v887f12,86100. So, I'm looking byte by byte through the received buffer (byte[1024 * 32]) and reading my data into the objects. So to get a number out of this I use the following code:



While program runs it takes up pretty much 100% of the processor yet it seems to be able to keep up with the incoming data (so far at least).

My question is, maybe there is a better way to do that? Also, why do loops in java take up 100% CPU?

Any input appreciated
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A compute-intensive loop in any language will take 100% of the CPU. There's no reason for it to pause or to wait for any other system component (i.e., disk, video card) so it just cranks through CPU instructions.

Given your problem setup, I think your code is fine.
 
Kris Philippaerts
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. The only reason for your code to pause is when it gets interrupted by a process with higher priority. And since most processes will have default priority, chances are small the code gets interrupted. It also depends a little bit on the JVM implementation and the OS you are working on, but in this case most environments will behave the same and give your code all the CPU it needs to finish the job as quickly as possible. (Some environments *might* interrupt your code for processes with the same priority as yours).
[ June 15, 2004: Message edited by: Kris Philippaerts ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is nextByte() implemented?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!