C # understanding Thread.Sleep () method ---- reprint

Reproduced

We may often use Thread.Sleep function to make a thread suspended for some time. Then you have not properly understood the use of this function do? Consider the following two issues:
1, assuming now 2008-4-712: 00: 00.000, if I call it Thread.Sleep (1000), in 2008-4-712: 00: 01.000 time, and this thread You will not wake up?
2, a person with a code word seemingly inexplicably: Thread.Sleep (0). Since it is a Sleep 0 milliseconds, so he removed compared with this code, so what's the difference?


 We first review the operating system works.
Operating system, CPU competition there are many strategies. Using a Unix system time slice algorithm, and Windows belongs preemptive.

In the time-slice algorithm, all processes arranged in a queue. Operating system according to their order, allocate some time to each process, the process that is allowed time to run. If the process is still running at the end of the time slice, the CPU will be deprived and assigned to another process. If the process before the end of the time slice blocked or ended, the CPU immediately switches. Scheduler to do is maintain a ready list of processes ,, when the process runs its time slice, it was moved to the end of the queue.

The so-called preemptive operating system, that is, if a process gets CPU time, unless it gives up the CPU use, otherwise it will completely occupy the CPU. Thus it can be seen in the preemptive operating system, the operating system assumes that all processes are "good character", it will take the initiative to withdraw CPU. In preemptive operating system, assuming there are several processes, the operating system will be based on their priority, starvation time (how long has not been used a CPU), calculated to give them an overall priority. CPU operating system will bring the total to the highest priority of this process. When the process is finished or suspend their own initiative, the operating system will recalculate a total priority of all processes, then pick the highest priority of the CPU control to him.


 We use the cake scene to describe these two algorithms. Suppose a steady stream of cake (a steady stream of time), a knife and fork (a CPU), 10 individuals waiting to eat cake (10 processes).

? If the Unix operating system is responsible for the cake, then he would set the rules: each person eat one minute, a time to replaced. After the last person to start again from scratch. So, no matter which 10 people is not different priorities, different levels of hunger, appetite different, each person can eat up time 1 minute. Of course, if someone had not very hungry, or a small appetite, eat eat after 30 seconds, then he can tell the operating system to say: I've had enough (hang). So let the operating system will be the next person then.

If the Windows operating system is responsible for the cake, then the scene is very interesting. He would set the rules: I'm going to calculate a priority to each of you according to your priority, the degree of hunger. The highest priority that person, you can come eat cake - you do not want to eat eat so far. And so the man finished, I re according to priority, the degree of hunger to calculate everyone's priority, and then points to the highest priority of the man.
It would appear that this scene it interesting - some people may be PPMM, and therefore have a high priority, so she can always come to eat cake. Another possibility is an ugly man, and go very ws, so the priority is particularly low, so quite a while and we have to turn to him again (because as time goes on, he will be more hungry, so the total count out the priority will be getting higher and higher, so one day his turn). Also, if accidentally let a big fat man got a knife and fork, because he was big appetite, maybe he ate the cake will occupy the long, long time, leading to the person next to where swallowing. . .
And, also there may be this is the case: the results of the operating system is now calculated, No. 5 PPMM always the highest priority, and a large cut above the others. So called No. 5 to eat cake. No. 5 eat for a little while, I feel not so hungry, so to say, "I do not eat" (hang). Therefore, the operating system will re-calculate the priority of all. Because the number 5 just eaten, so the extent of her hunger smaller, so the total priority smaller; while others wait for a while because of the extent of hunger are bigger, so the total priority is also larger. But this time it is still possible a higher priority than other No. 5, but now only a little higher than others - but she still is always the highest priority ah. So the operating system would say: No. 5 mm up to eat the cake ...... (No. 5 mm depressed heart, this is not just eat to lose weight ...... Well ...... people who told you long so beautiful, so get a higher priority) .

So, Thread.Sleep function is doing it? Also with the cake scene just described. The above scene inside, No. 5 MM after eating a cake, that has eight points full, and she felt over the next half an hour do not want to come back to eat the cake, then she would say with the operating system: in Do not call me within the next half an hour up the cake. In this way, when the operating system recalculates the total for all of the priority in the next half an hour inside, it ignores No. 5 mm. Sleep function is to do the job, he tells the operating system "in the next how many milliseconds CPU I do not participate in the competition."


 After reading the role of Thread.Sleep, let's think about two questions at the beginning of the article.

For the first question, the answer is: not necessarily. Because you just tell the operating system: In the next 1000 ms I do not want to participate in the CPU competition. So after the past, this time perhaps another thread is using 1000 ms CPU, so this time the operating system is not re-allocate CPU until the thread is suspended or ended; Moreover, even if this time happens to be the turn of the operating system CPU allocation , then the current thread is not necessarily always the highest priority, CPU or other threads may be preempted to go.

Similar to this, Thread has a Resume function, is used to awaken the suspended thread. As mentioned above, it seems, this function simply "tells the operating system from now on I began to participate in the competition of the CPU," calling this function does not immediately make this thread to acquire control of the CPU. (Note that this function in the future net2.0 mark and does not provide support in the follow-up version of the "obsolete")

For the second question, the answer is: Yes, but the difference is obvious. Suppose we just inside the cake scene, there are a number of additional PPMM 7, she's also very, very high priority (because it is very very beautiful), the operating system will always cried her cake. Moreover, the number 7 is also very fond of cake, but also a great appetite. However, No. 7 good character, she was very kind, she did not eat a few bites will think: If there are other people more than I need to eat cake, then I would give him. Therefore, she can bite each operating system just like to say: we have to re-calculate the total priority for all of it. However, the operating system does not accept the proposal - because the operating system does not provide this interface. So No. 7 mm and changed the statement: "In the next 0 milliseconds Do not call me up the cake." The operating system is to accept instruction, so the operating system at this time we will re-calculate the total priority - note that this is not even the 7th time calculated together, because "0 milliseconds has passed" them. So if no more than 7 people need to eat cake appeared, then the next 7 or cake will be called up.

Therefore, the role Thread.Sleep (0), is the "operating system immediately triggers a re-CPU competition." Perhaps the result of competition still get current thread CPU control, may be replaced by another thread gets control of the CPU. This is why we often write a Thread.Sleep (0) in a cycle which, because it gave other threads such as Paint thread gets control of the CPU power, so the interface will not be there in suspended animation.


 Finally explain that although the above-mentioned said, "unless it gives up the CPU use, otherwise the CPU will be fully occupied", but this behavior is still constrained - the operating system will monitor the situation you occupy the CPU, if we find a long thread occupation of CPU time, this will force the thread is suspended, so in fact the case, "a thread has been occupying the CPU and hold" does not appear. As for our large circulation caused by the program suspended animation, not because this thread has been occupying CPU. In fact, this time operating system've made a CPU competition, but other threads after a very short time to get control of the CPU immediately quit, would come the turn of this thread to continue the cycle, so he has with the operating system for a long time it was only forced to hang. . . Therefore, the reaction to the interface, it looks as if this thread has been occupying the same CPU.

Finally explain once again, the text threads, processes a bit confusing, in fact, Windows principle level, CPU are thread-level competition, the process here in this article, thread the same thing as enough.

Guess you like

Origin www.cnblogs.com/macT/p/12097178.html