In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how Thread.sleep (0) is used, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
We may often use the Thread.Sleep function to suspend the thread for a while. So do you have a correct understanding of the use of this function?
Consider the following two questions:
Suppose it is now 2020-05-24 1200 00.000. If I call Thread.Sleep (1000), will this thread be awakened at 2020-05-24 12? There is a seemingly inexplicable sentence in someone's code: Thread.Sleep (0). Since it's Sleep 0 milliseconds, what's the difference between it and getting rid of this code?
Let's first review the principle of the operating system.
In the operating system, there are many strategies for CPU competition. Unix system uses time slice algorithm, while Windows is preemptive. In the time slice algorithm, all processes are queued. According to their order, the operating system allocates a period of time to each process, that is, the time that the process is allowed to run. If the process is still running at the end of the time slice, the CPU is stripped and assigned to another process. If the process blocks or ends before the end of the time slice, CPU switches immediately. All the scheduling program needs to do is maintain a list of ready processes, and when the process runs out of its time slice, it is moved to the end of the queue.
The so-called preemptive operating system means that if a process gets CPU time, it will completely dominate CPU unless it gives up using CPU. Therefore, it can be seen that in the preemptive operating system, the operating system assumes that all processes are "very good character" and will actively exit CPU.
In a preemptive operating system, suppose there are several processes, and the operating system will give them an overall priority according to their priority and starvation time (how long it has been since they have not used CPU). The operating system will hand over the CPU to the process with the highest overall priority. When the process finishes execution or suspends itself, the operating system recalculates the total priority of all processes, and then chooses the one with the highest priority to give him control of CPU.
The Sleep function is to tell the operating system, "how many milliseconds in the future I will not compete in CPU."
For the first question, the answer is: not necessarily. Because you're just telling the operating system: I don't want to compete in CPU for the next 1000 milliseconds. Then 1000 milliseconds later, maybe another thread is using CPU, then the operating system will not reassign CPU until that thread suspends or ends; moreover, even if it happens to be the operating system's turn to allocate CPU, the current thread may not be the one with the highest total priority, and CPU may still be preempted by other threads.
Similarly, Thread has a Resume function that wakes up suspended threads. As mentioned above, this function simply "tells the operating system that I am competing for CPU from now on," and the call to this function does not immediately give the thread control over CPU.
For the second question, the answer is: yes, and the difference is obvious. The function of Thread.Sleep (0) is to "trigger the operating system to have a new CPU competition immediately". The result of the competition may be that the current thread still takes control of CPU, or it may be replaced by another thread to gain control of CPU. This is why we often write a Thread.Sleep (0) in a big loop, because this gives other threads such as Paint threads the right to gain control of CPU, so that the interface does not pretend to die there.
In addition, although it is mentioned above that "unless it gives up using CPU, it will completely occupy CPU", this behavior is still restricted-the operating system will monitor your occupation of CPU, and if you find that a thread has occupied CPU for a long time, it will force the thread to hang, so in fact, there will not be a situation in which "a thread has been occupying CPU all the time". As for our big loop causing the program to fake death, it is not because this thread has been occupying the CPU. In fact, the operating system has competed for CPU many times during this period of time, but other threads quit shortly after gaining control of CPU, so it was this thread's turn to continue to execute the loop, so it took a long time to be forced to suspend by the operating system. So when it comes to the interface, it looks as if the thread has been occupying CPU.
This is how Thread.sleep (0) is used. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.