Late at night, idle bored, sudden whim, want to write control CPU, let the CPU according to my thoughts, quack, because it is their own guess, do not spray oh , although no one to me comment.
Online to see a person said, a dead cycle can be a single-core CPU to get 100%, I do not believe, (My Computer i5 4200M), wrote a console program, the code is as follows
<span style= "color: #3366FF;" > static void Main (string[] args) {while (true) { } }</span>
and see
You see that 25, the top mark is CPU, should be CPU utilization, then, why not 100%? I sit in front of the computer to think (catch the hair, programmers should not catch hair, easy to drop).
Suddenly think of, people say right, because is the console program, so run is single-threaded, and my computer is a dual core four thread, then the CPU utilization is 25, if I open two threads, dead loop, CPU is 50. Three of them are 75. Four of them are 100.
Spicy, come down and start verifying that
Open two threads first
That's true, so come down and look at the third thread, the dead loop of four threads
Then you can do some of the things you want to do, such as using a program to draw a beat of the line, to use the Thread.Sleep (); This method can be used to control the CPU utilization at different times, next time to study, break sleep. Here's all the code
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace linecpu{ class program { static void Main (string[] args) { //Here is a test thread Th1 = new Thread (New Parameterizedthreadstart (fun)); Thread Th2 = new Thread (new Parameterizedthreadstart (fun)); Thread th3 = new Thread (new Parameterizedthreadstart (fun)); Thread Th4 = new Thread (new Parameterizedthreadstart (fun)); Th1. Start (); Th2. Start (); Th3. Start (); Th4. Start (); } private static void Fun (object obj) {while (true) { } }}}
C # thread Test CPU