Although I'm claiming to stride into the open source world, I don't know how to share anything without a few or two open source inks. Think about it. NET is not also considered open source almost, share the knowledge of C # is OK.
This article (series) is the knowledge of the popularization of the article, not professional, but to do not know where to start the small white, can play an auxiliary role.
All right, get to the chase.
Thread class Reference: Https://msdn.microsoft.com/zh-cn/library/system.threading.thread (v=vs.110). aspx
Start () is the start thread, sleep (int) is the current process hangs the specified number of times, abort () is to terminate the current process, jion () can make the parallel variable serial, let's try each one.
1 class Program2 {3 Static voidMain (string[] args)4 {5Thread T =NewThread (run);//This run is the name of the function, which is actually a delegate. 6T.start ();//run the thread first, and then run the output below. 7Console.WriteLine ("Test");8 Console.readkey ();9 }Ten Static voidRun () One { A intI=0; - while(I <999999) - { thei++; - } - Console.WriteLine (i); - } +}
The operating result is:
It is obvious that in the process of calculating a loop in a T-thread, the main thread (which is where the main function is) has already output the test first. This is the most basic multi-threading.
If you want to run the T thread and continue running the main thread after running Start (), you can add the Jion () function.
1 class Program2 {3 Static voidMain (string[] args)4 {5Thread T =NewThread (run);6 T.start ();7T.join ();//look here, this code is added here. 8Console.WriteLine ("Test");9 Console.readkey ();Ten } One Static voidRun () A { - intI=0; - while(I <999999) the { -i++; - } - Console.WriteLine (i); + } -}
The operating result is:
Compared to the earlier, the order is reversed.
Ok,sleep () and abort () I don't want to show it now, I'll show you one o'clock in the evening.
With these two demos, let's point to theoretical knowledge:
The operating system can not be calculated in parallel at first, such as sending a command to let the printer print a document, the operation of the printer, the operating system to wait until the printer finished printing to return the completed instructions before continuing to run the next program, which led to a lot of time the CPU is waiting for resources (printer's completion instructions), Rather wasteful. Later, the operating system designers designed a multi-process mechanism (in fact, the same as multithreading), each process is put into a running queue, when a process needs to wait for resources to continue, the process goes into a blocking state, is placed in another special blocking process in the queue. When the blocking process gets the required resources, the blocking state is lifted and put back into the running queue. When the process of multi-process environment, memory is not enough, this time in order to process information is not erased, the operating system for this design is called a suspended state, said the direct point is placed in the disk (hard disk) in the blocking queue, no different, Windows virtual memory have you heard? is to prepare for this. When an empty space is stored, the suspend process is awakened back into memory.
Multithreading the first article on this.
Multithreading in C # (1)