C# 多线程学习 四:本地vs共享状态

local 本地独立
CLR为每个线程分配自己的内存栈(stack) 以便是本地变量保持独立
for Text

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsolePlan
{
    class Program
    {
        private static TimeSpan SleepTime = new TimeSpan(0, 0, 1);
        private const string MainThreadName = "MainThread";
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = MainThreadName;
            Thread thread = new Thread(Text);
            thread.Name = "SubThread";
            thread.Start();

            Text();
            Console.ReadKey(true);
        }

        static void Text()
        {
            Console.WriteLine("【{0}】",Thread.CurrentThread.ManagedThreadId);
            for (int idx = 0; idx < 100; idx++)
            {
                if (Thread.CurrentThread.Name==MainThreadName)
                {
                    Console.Write(idx + "_main\t");  
                }
                else
                {
                    Console.Write(idx + "\t");  
                }
               
            }
            Console.WriteLine("---------------------------------------");
        }

    }
}

运行结果;在这里插入图片描述
结果分析:可知两个线程的变量是各个独立的。

Shared 共享:

  1. 如果多个线程都引用同一个对象的实例,那么他们就共享数据。
  2. 被lambda表达式或者匿名委托捕获的本地变量,会被编译器转化成字段(field) 所以也会被共享
  3. 静态字段(file)也会在线程间共享数据。

for Text one

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsolePlan
{
    class Program
    {
        private bool IsDone = false;

        private const string MainThreadName = "MainThread";
        static void Main(string[] args)
        {
            Program pr = new Program();
            Thread.CurrentThread.Name = MainThreadName;
            Thread thread = new Thread(pr.GoTo);
            thread.Name = "SubThread";
            thread.Start();

            //主线程调用方法
            pr.GoTo();
            Console.ReadKey(true);
        }

        void GoTo()
        {
            if (IsDone )
            {
                Console.WriteLine("?");
            }
            else
            {
                Thread.Sleep(100);
                IsDone = true;
                Console.WriteLine("#");
            }
        }
    }
}

如果在方法里线程不睡100ms结果又会不一样。用到共享资源需要临界边缘处理。加锁 lock(Isdone)

for text two

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsolePlan
{
    class Program
    {
        private const string MainThreadName = "MainThread";
        static void Main(string[] args)
        {
            bool IsDone = true;
            int i = 0;
            ThreadStart pro = () => {
                for (; i < 10; i++)
                {
                    Console.Write(i + "\t");
                }
                if (IsDone)
                {
                    IsDone = false;
                    Console.WriteLine(IsDone);
                }
            };
            Program pr = new Program();
            Thread.CurrentThread.Name = MainThreadName;
            Thread thread = new Thread(pro);
            thread.Name = "SubThread";
            thread.Start();

            //主线程调用方法
            pro();
            Console.ReadKey(true);
        }
    }
}

运行结果截图:
在这里插入图片描述
可以看出在主线程进入方法匿名委托以后,主方法的内的i 和IsDone被捕获,第二次进来以后变量值没有释放。
反编译可以看出源码编译的代码:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值