ASP.NET:一段比較經典的多線程學習代碼

來源:互聯網
上載者:User
asp.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();

僅做學習討論。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.