ASP.NET: a classic piece of multi threaded learning code

  • 2020-05-05 11:07:19
  • OfStack

A more classic multi - threaded learning code.

1. The synchronization problem of multi-threading is used.
2, the use of multi-threaded sequence problem.

If you are interested, please read the following code carefully. Notice the order of the pieces of code, and think about, can the order of the pieces of code be reversed? Why? This should be very helpful to study. For demonstration purposes, all threads were Sleep for a while.

using System.Net;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace Webb.Study
{
      class TestThread
      {
              static Mutex m_Mutex                       = new Mutex();
              static Thread[] m_testThreads       = new Thread[10];
              static int m_threadIndex               = 0;

              static void ThreadCallBack()
              {
                      TestThread.m_Mutex.WaitOne();
                      int m_index       = m_threadIndex;
                      TestThread.m_Mutex.ReleaseMutex();
                      Console.WriteLine("Thread {0} start.",m_index);
                      for(int i=0;i < =10;i++)
                      {
                              TestThread.m_Mutex.WaitOne();        
                              Console.WriteLine("Thread {0}: is running. {1}",m_index,i);
                              TestThread.m_Mutex.ReleaseMutex();
                              Thread.Sleep(100);
                      }
                      Console.WriteLine("Thread {0} end.",m_index);
              }

              public static void Main(String[] args)
              {
                      Console.WriteLine("Main thread start.");
                      for(int i=0;i < TestThread.m_testThreads.Length;i++)
                      {
                              TestThread.m_threadIndex       = i;
                              TestThread.m_testThreads[i]       = new Thread(new ThreadStart(ThreadCallBack));                              
                              TestThread.m_testThreads[i].Start();
                              Thread.Sleep(100);
                      }
                      for(int i=0;i < TestThread.m_testThreads.Length;i++)
                      {
                              TestThread.m_testThreads[i].Join();
                      }
                      Console.WriteLine("Main thread exit.");
              }
      }
}

1. Can these two sentences be exchanged in the main function? Why is that?

                              TestThread.m_testThreads[i].Start();
                              Thread.Sleep(100);

2. Can these two sentences be used interchangeably in the CallBack function? Why is that? What's the difference?

                              TestThread.m_Mutex.ReleaseMutex();
                              Thread.Sleep(100);

3. Can the main function be written like this? Why is that? What's the difference?

              public static void Main(String[] args)
              {
                      Console.WriteLine("Main thread start.");
                      for(int i=0;i < TestThread.m_testThreads.Length;i++)
                      {
                              TestThread.m_threadIndex       = i;
                              TestThread.m_testThreads[i]       = new Thread(new ThreadStart(ThreadCallBack));                              
                              TestThread.m_testThreads[i].Start();
                              TestThread.m_testThreads[i].Join();
                              Thread.Sleep(100);
                      }
                      Console.WriteLine("Main thread exit.");
              }

4. What does this sentence do? So what's the problem with the program? What changes should be made?

    TestThread.m_Mutex.WaitOne();
    int m_index = m_threadIndex;
    TestThread.m_Mutex.ReleaseMutex();

Just do the study discussion.

 


Related articles: