Multithreading programming learning notes-basics (I), multithreading programming learning notes
I. Introduction to multithreading
C # code that supports parallel execution through multiple threads. A thread is a path for independent execution and can be run with other threads at the same time. A c # Client Program (Console, WPF, Winows Forms) starts from a separate thread, which is automatically created by CLR and the operating system. We call it the main thread, in addition, you can create additional threads to implement multithreading.
Ii. Preliminary Exploration
1. Create the first multi-threaded Program
2. Open visual studio 2015. Create a new console application ThreadConsoleApp. For example.
3. This project uses. net framework 4.5.2.
4. The Code is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; // introduce the thread namespace ThreadConsoleApp {class Program {static void Main (string [] args) {// create a thread to run PrintNumber. When we construct a thread, threadStart or ParameterizedThreadStart instance delegation will be passed to the constructor // we only need to specify the names of the methods to be run by different threads, as shown below. The compiler automatically creates these objects in the background. Thread t = new Thread (PrintNumber); // start Thread t. start (); PrintNumber (); Console. read ();} static void PrintNumber () {Console. writeLine ("the first multi-threaded program starts .... "); For (int I = 0; I <20; I ++) {long time = DateTime. now. ticks; Console. writeLine (string. format ("Ticks: {0 }=={ 1}", time, I ));}}}}
5. The program running result is as follows:
From the result graph, we can see that the two groups of numbers with a value of 20 appear randomly. This shows that the PrintNumber method runs in the main thread and runs in the subthread.
3. Thread Suspension)
1. The Code is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; // introduce the Thread namespace ThreadConsoleApp {class Program {static void Main (string [] args) {Thread t = new Thread (PrintNumberDely); // start Thread t. start (); PrintNumber (); Console. read ();} static void PrintNumber () {Console. writeLine ("the first multi-threaded program starts .... Second: "+ DateTime. now. second); for (int I = 0; I <10; I ++) {Console. writeLine (string. format ("{0}", I) ;}/// <summary> /// Method for suspending 2 seconds /// </summary> static void PrintNumberDely () {Console. writeLine ("the first multi-thread pause starts .... "); For (int I = 0; I <10; I ++) {Console. writeLine (string. format ("Second: {0 }=={ 1}", DateTime. now. second, I); Thread. sleep (TimeSpan. fromSeconds (2 ));}}}}
2. The result is as follows.
Figure 1
Figure 2
View the result chart, as shown in Figure 2 above. When the program runs, create a sub-thread to run the PrintNumberDely () method, and then execute the PrintNumber. However, because the PrintNumberDely method has a pause code, a number is printed every 2 seconds.
4. Join)
1. The Code is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; // introduce the thread namespace ThreadConsoleApp {class Program {static void Main (string [] args) {Console. writeLine ("START"); Thread t = new Thread (PrintNumberDely); // start Thread t. start (); // The thread waits for t. join (); Console. writeLine ("end"); Console. read () ;}/// <summary> /// the method for suspending 2 seconds /// </summary> static void PrintNumberDel Y () {Console. WriteLine ("the first multithreading waits for the program to start .... "); For (int I = 0; I <10; I ++) {Console. writeLine (string. format ("Second: {0 }=={ 1}", DateTime. now. second, I); Thread. sleep (TimeSpan. fromSeconds (2 ));}}}}
2. The program running result is shown in.
As shown in the result chart, when the program is running, create a sub-thread to run the PrintNumberDely () method. However, because t. join () method, so the main thread will be blocked until the sub-thread execution is complete, the main thread will continue to execute, and finally print "end ". This technology can be used to implement synchronous execution of two threads. The first thread can continue execution only after the execution of another thread is completed.
Annotate the join () method. The running result of the program is shown in. Compare the differences between the two results.