In working in New York and talking to programmers all over Wall Street, I've noticed a common thread of knowledge expected in most real time programming applications. That knowledge is known as multithreading. As I have migrated around the programming world, and performed interviews on potential programming candidates, it never ceases to amaze me how little is known about multithreading or why or how threading is applied. In a series of excellent articles written by Vance Morrison, MSDN has tried to address this problem: (See the August Issue of MSDN, What every Developer Must Know about Multithreaded Apps, and the October issue Understand the Impact of Low-Lock Techniques in Multithreaded Apps.
What is a thread?
What are multithreaded applications?
An Unusual Analogy
When to Thread
When not to Thread

Problems with Threading
Thread Safety
- // shared memory variable between the two threads
- // used to indicate which thread we are in
- private string _threadOutput = "";
- /// <summary>
- /// Thread 1: Loop continuously,
- /// Thread 1: Displays that we are in thread 1
- /// </summary>
- void DisplayThread1()
- {
- while (_stopThreads == false)
- {
- Console.WriteLine("Display Thread 1");
- // Assign the shared memory to a message about thread #1
- _threadOutput = "Hello Thread1";
- Thread.Sleep(1000); // simulate a lot of processing
- // tell the user what thread we are in thread #1, and display shared memory
- Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
- }
- }
- /// <summary>
- /// Thread 2: Loop continuously,
- /// Thread 2: Displays that we are in thread 2
- /// </summary>
- void DisplayThread2()
- {
- while (_stopThreads == false)
- {
- Console.WriteLine("Display Thread 2");
- // Assign the shared memory to a message about thread #2
- _threadOutput = "Hello Thread2";
- Thread.Sleep(1000); // simulate a lot of processing
- // tell the user we are in thread #2
- Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
- }
- }
- Class1()
- {
- // construct two threads for our demonstration;
- Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
- Thread thread2 = new Thread(new ThreadStart(DisplayThread2));
- // start them
- thread1.Start();
- thread2.Start();
- }

Explanation
Winning the Race
- /// <summary>
- /// Thread 1, Displays that we are in thread 1 (locked)
- /// </summary>
- void DisplayThread1()
- {
- while (_stopThreads == false)
- {
- // lock on the current instance of the class for thread #1
- lock (this)
- {
- Console.WriteLine("Display Thread 1");
- _threadOutput = "Hello Thread1";
- Thread.Sleep(1000); // simulate a lot of processing
- // tell the user what thread we are in thread #1
- Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
- }// lock released for thread #1 here
- }
- }
- /// <summary>
- /// Thread 1, Displays that we are in thread 1 (locked)
- /// </summary>
- void DisplayThread2()
- {
- while (_stopThreads == false)
- {
- // lock on the current instance of the class for thread #2
- lock (this)
- {
- Console.WriteLine("Display Thread 2");
- _threadOutput = "Hello Thread2";
- Thread.Sleep(1000); // simulate a lot of processing
- // tell the user what thread we are in thread #1
- Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
- } // lock released for thread #2 here
- }
- }

Alternative Solution
- AutoResetEvent _blockThread1 = new AutoResetEvent(false);
- AutoResetEvent _blockThread2 = new AutoResetEvent(true);
- /// <summary>
- /// Thread 1, Displays that we are in thread 1
- /// </summary>
- void DisplayThread_1()
- {
- while (_stopThreads == false)
- {
- // block thread 1 while the thread 2 is executing
- _blockThread1.WaitOne();
- // Set was called to free the block on thread 1, continue executing the code
- Console.WriteLine("Display Thread 1");
- _threadOutput = "Hello Thread 1";
- Thread.Sleep(1000); // simulate a lot of processing
- // tell the user what thread we are in thread #1
- Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
- // finished executing the code in thread 1, so unblock thread 2
- _blockThread2.Set();
- }
- }
- /// <summary>
- /// Thread 2, Displays that we are in thread 2
- /// </summary>
- void DisplayThread_2()
- {
- while (_stopThreads == false)
- {
- // block thread 2 while thread 1 is executing
- _blockThread2.WaitOne();
- // Set was called to free the block on thread 2, continue executing the code
- Console.WriteLine("Display Thread 2");
- _threadOutput = "Hello Thread 2";
- Thread.Sleep(1000); // simulate a lot of processing
- // tell the user we are in thread #2
- Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
- // finished executing the code in thread 2, so unblock thread 1
- _blockThread1.Set();
- }
- }
Conclusion
來源: https://www.c-sharpcorner.com/article/introduction-to-multithreading-in-C-Sharp/






