Home > Java > JavaBase > body text

What is the function of thread.sleep?

hzc
Release: 2020-06-15 11:52:40
Original
7901 people have browsed it

What is the function of thread.sleep?What is the role of thread.sleep:

(1) CPU competition strategy

In the operating system, there are many strategies for CPU competition, Unix The system uses a time slice algorithm, while Windows uses a preemptive algorithm.

(1).

In the time slice algorithm, all processes are arranged in a queue. The operating system allocates a period of time to each process in their order, that is, the time that the process is allowed to run. If the process is still running when the time slice ends, the CPU will be taken away and assigned to another process. If the process blocks or ends before the end of the time slice, the CPU switches immediately. All the scheduler has to do is maintain a list of ready processes, and when a process uses up its time slice, it is moved to the end of the queue.

(2).

The preemptive operating system means that if a process gets CPU time, it will completely occupy the CPU unless it gives up using the CPU. Therefore, it can be seen that in a preemptive operating system, the operating system assumes that all processes are of "good character" and will actively exit the CPU. In a preemptive operating system, assuming there are several processes, the operating system will calculate a total priority for them based on their priorities and starvation time (how long it has not been using the CPU). The operating system will give the CPU to the process with the highest overall priority. When the process completes execution or actively hangs, the operating system will recalculate the total priority of all processes, and then select the one with the highest priority to give him control of the CPU.

(2) According to the above, it can be concluded that the function of Thread.Sleep() is to tell the operating system "I will not participate in CPU competition in the next few milliseconds";

Then there are two Question:

(1).What is the function of Thread.Sleep(0)?

The function of Thread.Sleep(0) is to "trigger the operating system to immediately re-start a CPU competition". The result of the competition may be that the current thread still obtains CPU control, or it may be replaced by another thread to obtain CPU control. This is why we often write Thread.Sleep(0) in large loops, because this gives other threads such as the Paint thread the power to obtain CPU control, so that the interface will not freeze there.

(2). Assuming that Thread.Sleep(1000) is called now, will this thread definitely be awakened after 1 second?

The answer is: not necessarily. Because you just tell the operating system: I don't want to participate in CPU competition in the next 1000 milliseconds. Then after 1000 milliseconds passes, another thread may be using the CPU at this time, so the operating system will not reallocate the CPU until that thread hangs or ends; besides, even if it happens to be the operating system's turn to allocate the CPU at this time , then the current thread is not necessarily the one with the highest overall priority, and the CPU may still be preempted by other threads.

Recommended tutorial: "java tutorial"

The above is the detailed content of What is the function of thread.sleep?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!