有趣的多線程編程(3)——線程內部是如何進行的?

來源:互聯網
上載者:User
編程|多線程
看一下以下兩個例子的運行結果:
//TestThread.cs
using System;using System.Threading;public class Test{ static int count=0; static void Main() { ThreadStart job = new ThreadStart(ThreadJob); Thread thread = new Thread(job); thread.Start(); for (int i=0; i < 5; i++) { count++; } thread.Join(); Console.WriteLine ("Final count: {0}", count); } static void ThreadJob() { for (int i=0; i < 5; i++) { count++; } }}
//InnerDataThread.cs
using System;using System.Threading;public class Test{    static int count=0;        static void Main()    {        ThreadStart job = new ThreadStart(ThreadJob);        Thread thread = new Thread(job);        thread.Start();                for (int i=0; i < 5; i++)        {            int tmp = count;            Console.WriteLine ("Read count={0}", tmp);            Thread.Sleep(50);            tmp++;            Console.WriteLine ("Incremented tmp to {0}", tmp);            Thread.Sleep(20);            count = tmp;            Console.WriteLine ("Written count={0}", tmp);            Thread.Sleep(30);        }                thread.Join();        Console.WriteLine ("Final count: {0}", count);    }        static void ThreadJob()    {        for (int i=0; i < 5; i++)        {            int tmp = count;            Console.WriteLine ("\t\t\t\tRead count={0}", tmp);            Thread.Sleep(20);            tmp++;            Console.WriteLine ("\t\t\t\tIncremented tmp to {0}", tmp);            Thread.Sleep(10);            count = tmp;            Console.WriteLine ("\t\t\t\tWritten count={0}", tmp);            Thread.Sleep(40);        }    }}
  
Read count=0                                Read count=0                                Incremented tmp to 1                                Written count=1Incremented tmp to 1Written count=1                                Read count=1                                Incremented tmp to 2Read count=1                                Written count=2                                Read count=2Incremented tmp to 2                                Incremented tmp to 3Written count=2                                Written count=3Read count=3                                Read count=3Incremented tmp to 4                                Incremented tmp to 4                                Written count=4Written count=4                                Read count=4Read count=4                                Incremented tmp to 5                                Written count=5Incremented tmp to 5Written count=5Read count=5Incremented tmp to 6Written count=6Final count: 6

再比較下面這個例子:

//使用Monitor.Enter/Exit
//MonitorThread.cs

using System;
using System.Threading;

public class Test
{
    static int count=0;
    static readonly object countLock = new object();
   
    static void Main()
    {
        ThreadStart job = new ThreadStart(ThreadJob);
        Thread thread = new Thread(job);
        thread.Start();
       
        for (int i=0; i < 5; i++)
        {
            Monitor.Enter(countLock);
            int tmp = count;
            Console.WriteLine ("Read count={0}", tmp);
            Thread.Sleep(50);
            tmp++;
            Console.WriteLine ("Incremented tmp to {0}", tmp);
            Thread.Sleep(20);
            count = tmp;
            Console.WriteLine ("Written count={0}", tmp);
            Monitor.Exit(countLock);
            Thread.Sleep(30);
        }
       
        thread.Join();
        Console.WriteLine ("Final count: {0}", count);
    }
   
    static void ThreadJob()
    {
        for (int i=0; i < 5; i++)
        {
            Monitor.Enter(countLock);
            int tmp = count;
            Console.WriteLine ("\t\t\t\tRead count={0}", tmp);
            Thread.Sleep(20);
            tmp++;
            Console.WriteLine ("\t\t\t\tIncremented tmp to {0}", tmp);
            Thread.Sleep(10);
            count = tmp;
            Console.WriteLine ("\t\t\t\tWritten count={0}", tmp);
            Monitor.Exit(countLock);
            Thread.Sleep(40);
        }
    }
}

結果與上例InnerDataThread.cs是不一樣的,原因就在於Monitor的使用了。

Read count=0Incremented tmp to 1Written count=1                                Read count=1                                Incremented tmp to 2                                Written count=2Read count=2Incremented tmp to 3Written count=3                                Read count=3                                Incremented tmp to 4                                Written count=4Read count=4Incremented tmp to 5Written count=5                                Read count=5                                Incremented tmp to 6                                Written count=6Read count=6Incremented tmp to 7Written count=7                                Read count=7                                Incremented tmp to 8                                Written count=8Read count=8Incremented tmp to 9Written count=9                                Read count=9                                Incremented tmp to 10                                Written count=10Final count: 10

下面使用lock來鎖定線程:
// LockThread.cs
using System;
using System.Threading;

public class Test
{
    static int count=0;
    static readonly object countLock = new object();
   
    static void Main()
    {
        ThreadStart job = new ThreadStart(ThreadJob);
        Thread thread = new Thread(job);
        thread.Start();
       
        for (int i=0; i < 5; i++)
        {
            lock (countLock)
            {
                int tmp = count;
                Console.WriteLine ("Read count={0}", tmp);
                Thread.Sleep(50);
                tmp++;
                Console.WriteLine ("Incremented tmp to {0}", tmp);
                Thread.Sleep(20);
                count = tmp;
                Console.WriteLine ("Written count={0}", tmp);
            }
            Thread.Sleep(30);
        }
       
        thread.Join();
        Console.WriteLine ("Final count: {0}", count);
    }
   
    static void ThreadJob()
    {
        for (int i=0; i < 5; i++)
        {
            lock (countLock)
            {
                int tmp = count;
                Console.WriteLine ("\t\t\t\tRead count={0}", tmp);
                Thread.Sleep(20);
                tmp++;
                Console.WriteLine ("\t\t\t\tIncremented tmp to {0}", tmp);
                if (count < 100)
                    throw new Exception();
                Thread.Sleep(10);
                count = tmp;
                Console.WriteLine ("\t\t\t\tWritten count={0}", tmp);
            }
            Thread.Sleep(40);
        }
    }
}

結果如何?與MonitorThread.cs比較一下,再想想看。



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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