C # thread Test CPU

Source: Internet
Author: User

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

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.