Java threads - CPU usage

Here is a quote from the tutorial that I am reading at the moment:

"That is, whenever a thread needs to execute a loop with a large number of iterations, sleep () follows at each iteration - The duration of a short sleep for events, for example 5 milliseconds, can reduce the overall CPU utilization of the application from 100% to> 1%"

This is a good practice, I believe, but; not the scheduler does just that - a little time for thread1; suspend thread1; A little time for thread2 ... etc. I can’t understand such a fall rate, who wants to enlighten me?

+4
source share
5 answers

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) { /* we tried */} } } 

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.

+5
source

The scheduler does just that.
The difference is that the scheduler is doing it right. This means that it will use the processor efficiently and therefore you will get good CPU utilization.

I see nothing wrong with that. It just means that it works.

When you let it sleep, there will be more downtime and you will reduce CPU usage. If this is for some reason. High CPU utilization is not harmful (if you do not overheat, in this case you have problems with the equipment).

Usually, when I come to multi-threaded issues, I actually aim for high CPU utilization. This usually means that the problem is split evenly between threads, and I get the most out of the CPU.

If you use 1% of the processor, it means that it does not work, so why do you have such a good computer? You must use the equipment.

+2
source

When your program performs hashing (or other tasks with an intensive processor), you want it to run at 100% speed, right?

OTOH, if your program is waiting for input, then you should use asynchronous programming as much as possible and not run endlessly in a loop (asynchronously = the system calls you).

+2
source

Forget about it. What limits CPU usage to 1%? Nothing at all?

Limiting CPU usage by a factor of 100 means, in general, slowing down the application by a factor.

You may want it if there are other more important threads, or if you can stop this thread using Thread.interrupt() , but both can be achieved differently.

0
source

I have never heard of such a practice, however this is likely to lead to a huge decrease in CPU usage. This is due to the fact that the "bit" of time that the scheduler gives for each thread is very small, so 5 ms is a significant amount of time comparable to this.

With that said, I see one place you could benefit from such a slowdown: responsiveness on single-core machines.

0
source

Source: https://habr.com/ru/post/1345618/


All Articles