You see this a lot in programs that update the display of something. It caused a wait, and it is bad.
If you have a loop that does something like this
public void run() { while(running) { gui.render(); } }
You are going to chew on your processor when you really don't need it. Do you need to do this again and again, 100,000+ times per second? No, you really need only about 30 frames per second.
public void run() { while(running) { gui.render(); try { Thread.sleep(10); } catch(InterruptedException e) { } } }
This will limit you to just under 100 frames per second and you will get much better performance.
You do not always want this for intensive processor threads, since you want them to take precedence. Moreover, if your background occupies the entire processor, how will you process further input (for example, I do not know the CANCEL button because you did not want to start this intensive hourly calculation?)
So, adding a little sleep to your threads MAY be a very good solution.
source share