multithreading - Java threads - cpu usage -


Here is a quote from a textbook that I am currently reading:

" That is, whenever a thread is required to execute a loop with many iterations, then good practice is to insert sleep () in every repetition - event less sleep, such as 5 milliseconds, the application's overall CPU Can reduce usage by 100% to 1% "

This is a good practice, my value Security is, however; Scheduler does not do this at all - some time for thread1 ; Suspended thread1 ; A little time for thread 2 ... etc. I can not understand the drop rate, which I want to highlight?

If you see a lot of it, programs that update the performance of something called a busy wait, And that's bad.

If you have a loop that does something like this

  running a public null () {while (running) {gui.render (); }}   

You're going to chew your CPU when you do not really need it. Do you need to render it, 100000+ times a second? No, you really only need about 30 frames for a second.

  Public domain runs () {while (running) {gui.render (); Try {Thread.sleep (10); } Hold (erected e) We have tried} /}}}   

This will limit you to 100 frames per second, and you will end up in a much better way .

You do not always want this for the processor's intense background thread, because you want it to be said to prioritize that, if your background takes all the CPUs, then how will you process further input ( Like, I do not know, a cancell button because you did not mean to start that intense, hour-long count?)

So adding a little sleep to your sources can be a very good decision.

Comments