This is a generic question that I've always wondered.
In general, is it intensive for a CPU to create threads that perform "while not true ..." loops or similar?
For example, suppose I do:
// pseudo-code
new Thread(function() {
while (!useHasDoneSomething) {
// do nothing...
}
alert("you did something!");
}
So it's just a piece of code that runs in a thread that waits for something to happen.
For whatever reason I imagine that this would pressure the CPU. After all, even though it's just one little loop, it's re-running the loop the instant that a cycle is done. Wouldn't this mean that it's going through the cycle many times per millisecond? ... Per nanosecond!?
The operating system will "handle" the program and the thread(s), right? But that's still an awful lot of attention to running a loop. If the CPU has nothing else to do, wouldn't it just focus on that loop, thereby consuming all the idle time?
What if I create 1000 threads that all do the same loop? How might that affect performance/CPU cycles?
Furthermore, is this how events function internally? When you're 'listening' for an event in Java, .NET, Javascript etc (such as a button press or a window resize), has a thread that loops been created?