国产探花免费观看_亚洲丰满少妇自慰呻吟_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比較一下,再想想看。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东台市| 洞口县| 鄯善县| 乐都县| 尖扎县| 霍城县| 个旧市| 南昌县| 红河县| 长治县| 双鸭山市| 昌吉市| 剑阁县| 平谷区| 永年县| 镇巴县| 镇沅| 比如县| 木里| 汝州市| 正镶白旗| 成安县| 台北县| 青铜峡市| 周宁县| 冕宁县| 安阳市| 乌苏市| 无棣县| 鹿邑县| 辽宁省| 山西省| 德保县| 遂川县| 玉屏| 蓝田县| 黎川县| 依安县| 六枝特区| 青川县| 兰坪|