zoukankan      html  css  js  c++  java
  • <转载>.NET:一段比较经典的多线程学习代码

    一段比较经典的多线程学习代码。
      1、用到了多线程的同步问题。
      2、用到了多线程的顺序问题。

      如果有兴趣的请仔细阅读下面的代码。注意其中代码段的顺序,思考一下,这些代码的顺序能否互相调换,为什么?这应该对学习很有帮助的。为了演示,让所有的线程都Sleep了一段时间。


    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、主函数中这两句能否互换?为什么?

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

      2、CallBack函数中这两句能否互换?为什么?会有什么不同的结果?

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

      3、主函数能否写成这样?为什么?会有什么不同的结果?

              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、这几句的作用是什么?那么程序中还存在什么样的问题?应该做怎样的修改?

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

  • 相关阅读:
    小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载一(PhoneGap中的API)
    有一种蓝,是神往,是心醉,是心伤
    persits.jpeg 水印组件
    SD卡操作相关的工具SDCardUtils
    apollo 消息分发源代码分析
    tcp ip协议笔记(1)——简单介绍
    百度地图SDK调试SDKInitializer.initialize(getApplicationContext())错误
    一气呵成编完代码的感觉对不正确
    多线程编程1-NSThread
    VIP的转移
  • 原文地址:https://www.cnblogs.com/ChangTan/p/2050398.html
Copyright © 2011-2022 走看看