C # thread (i)

Source: Internet
Author: User

Brief introduction

in the past, ordinary computers had only computational units, but could not perform multiple tasks at the same time. However, the operating system has been able to run multiple applications at the same time, realizing the concept of multitasking. To prevent one application from controlling the CPU and causing other applications and the operating system itself to be suspended forever, the operating system has to use some way to divide the physical computing cells into virtual processes and give each program a certain amount of computing power. In addition, the operating system must always have priority access to the CPU and can adjust the priority of different programs to access the CPU. Threading is the realization of this concept. You can think of a thread as a virtual process for running a specific program on its own. (keep in mind that threads consume a lot of operating system resources.) Multiple threads sharing a physical processor will cause the operating system to be busy managing these threads and cannot run the program. )

Next, create a console application

class  Program         {         static void Main (string[] args)        {         threadnewthread(printnumbers) ;            T.start ();            Printnumbers ();            Console. ReadKey ();        }         static void Printnumbers () {            Console. WriteLine ("Starting ...");             for (int i = 1; i < ten; i++)            {                Console. WriteLine (i);}}         }     
Results two sets of numbers ranging from 1 to 10 are randomly crossed out. This shows that the Printnumbers method runs concurrently between the main thread and the other.
        static void DoNothing () {            Thread. Sleep (TimeSpan. FromSeconds (2));        }        static void Printnumberswithstatus ()        {            Console. WriteLine ("Starting ...");            Console. WriteLine (Thread. CurrentThread.ThreadState.ToString ());             for (int i = 1; i < ten; i++) {            Thread. Sleep (TimeSpan. FromSeconds (2));            Console. WriteLine (i);            }        }    

Add the following code snippet to the Main method

            Console. WriteLine ("Starting program ...");Threadt =New Thread(Printnumberswithstatus);Threadt2 =New Thread(DoNothing);Console.            WriteLine (T.threadstate.tostring ()); T2.            Start (); T.start (); for(inti = 1; I < 30; i++) {Console.            WriteLine (T.threadstate.tostring ()); }Thread. Sleep (TimeSpan.            FromSeconds (6)); T.abort ();Console. WriteLine ("A thread has been aborted");Console. WriteLine (T.threadstate.tostring ());Console. WriteLine (T2. Threadstate.tostring ());
How it works when the main program starts, it defines two different threads. One will be terminated and the other will run successfully. The thread state is in the ThreadState property of the Thread object. The ThreadState property is a C # enumeration object. The initial thread state is threadstate.unstarted. Then we start the thread and estimate that the thread state changes from threadstate.running to Threadstate.waitsleepjoin in the interval of 30 iterations of a cycle. If the actual situation does not match the above, increase the number of iterations, after terminating the first thread, you will see that the thread state is now threadstate.aborted. The program may also print out the threadstate.abortrequested status. This fully illustrates the complexity of synchronizing two threads. Remember that you do not use thread termination in your program. I'm using him here just to show the corresponding thread state. Finally, you can see that the second thread T2 completed successfully and has a status of threadstate.stopped. There are other thread states, but they are either deprecated or not useful in several states that we have tried.

C # thread (i)

Related Article

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.