国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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

2024-07-21 02:29:41
字體:
來源:轉載
供稿:網友
看一下以下兩個例子的運行結果:
//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比較一下,再想想看。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 木里| 左权县| 盐池县| 天祝| 邵阳县| 星子县| 朔州市| 大余县| 永宁县| 睢宁县| 宿松县| 新巴尔虎左旗| 遂昌县| 甘肃省| 昔阳县| 波密县| 黎城县| 沧州市| 丘北县| 铜梁县| 会宁县| 台中市| 呈贡县| 抚远县| 洱源县| 怀仁县| 阳朔县| 天祝| 左权县| 台南县| 平利县| 衡南县| 岳阳市| 文水县| 隆林| 昌平区| 宿州市| 龙海市| 广宁县| 佛山市| 南充市|