Forums Register Login

100% CPU usage or bad code

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
How is nextByte() implemented?
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 804 times.
Similar Threads
unzip problem, I give up now
Multi Files Transfer from client machines to server machines using socket communication in java
Finding the largest Prime Factor
recursive method
OutOfMemoryError when accessing Zip File
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:51:31.