Discuss thread. Sleep (0) causes high CPU usage

Source: Internet
Author: User
Tags high cpu usage

First, we need to understand the related concepts:

1. CPU usage is actually the CPU resources occupied by the program you run, indicating the running program of your machine at a certain time point. Higher usage

High, indicating that your machine runs a lot of programs at this time, and vice versa. The usage is directly related to your CPU strength. Modern time-sharing

Multi-task operating systems use time slice for CPUs: for example, process a occupies 10 ms, process B occupies 30 ms, and then idle for 60 ms.

Process a accounts for 10 ms, process B accounts for 30 ms, and idle for 60 ms. If this is the case for a period of time, the occupied rate during this period is 40%. CPU

The response to the thread is not consecutive. Generally, the thread is automatically interrupted after a period of time. When the number of threads that do not respond increases, the CPU usage increases.

From Baidu encyclopedia

2. thread. Sleep (0) is not used simply because the thread has slept for 0 ms. It will hard trigger a CPU action ------- calculate the priority of all waiting threads

Level to determine the winner of the next CPU control.


Well, this small action theoretically has a good impact on preemptible threads:

1. Avoid occupying CPU resources for a thread for a long time

2. The thread is not suspended.

3. Give threads a relatively average chance to obtain CPU resources


However, a problem was found recently. When sleep (0) is added to the thread of a multi-threaded platform, there are two situations:

1. When the thread runs falsely, the CPU usage is very high or even full, but the thread is not dead and the platform is not dead;

2. When all threads are running the task, the CPU usage is not high and it is normal. So why is this happening?


I understand it as follows:

1. Dry run of the thread: Because sleep (0) is added to each thread, the only action of the thread is to trigger the CPU to calculate the priority and continue to calculate the CPU.

Basically, there is no idle time, resulting in high CPU usage.

2. thread running tasks: there is a period of time between the start of the thread and the end of the thread, and the interval may be long or short according to different environment conditions, that is, in the thread

During the running time, the CPU computing priority will not be triggered. In this way, the computing frequency is reduced and the interval increases. In addition, when a large number of threads run again, wait

The number of threads is less or even running, so that the CPU idle time is out, so the CPU usage is not high.


Therefore, thread. Sleep (0) is a good thing, but it must be in the right place to play its role well.

The first thing we can determine is that thread. Sleep (0) is relatively resource-consuming, so it cannot be abused. The above example is abuse and we put sleep (0)

Each thread does not need to frequently calculate the priority of the waiting thread. It will not only result in polarization, but also consume resources.


This is my idea;

Create a thread other than a working thread, which is responsible for this task at regular intervals. This minimizes the consumption and ensures the healthy storage of the working thread.

Active, so that the CPU usage will be relatively stable when idle or busy.


(Personal thoughts, if you have different ideas, hope to communicate)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.