美文网首页
ThreadLocal

ThreadLocal

作者: 小狼在IT | 来源:发表于2018-05-22 13:02 被阅读0次
    public class Test2
    {
        private List<string> lst1;
        public Test2()
        {
            lst1 = new List<string>();
        }
    
        public List<string> getList()
        {
            return lst1;
        }
        public void setList(List<string> l)
        {
            lst1 = l;
    
        }
    }
    

    调用:创建一个Test2对象,给里面的lst1赋值,启动一个线程,也给里面的lst1再赋值。

            ThreadLocalTest.Test2 t11 = new ThreadLocalTest.Test2();
            t11.setList(new List<string>() { "a1", "b1", "c1" });
    
            Thread t = new Thread(() =>
            {
                t11.setList(new List<string>() { "aa", "bb", "cc" });
                foreach (string a in t11.getList())
                {
                    Console.WriteLine("Thread:" + a);
                }
            });
            t.Start();
            t.Join();
    
            foreach (string a in t11.getList())
            {
                Console.WriteLine(a);
            }
    
    image.png

    从结果看没什么特别,新的赋值把旧的赋值覆盖掉了。
    ---------------------分割线------------------------
    这次将List<string>用ThreadLocal来定义。
    例子1:

    public class Test1
    {
        private readonly ThreadLocal<List<string>> _localCtx = new ThreadLocal<List<string>>(() => new List<string>());
    
        public List<string> getList()
        {
            return _localCtx.Value;
        }
        public void setList(List<string> l)
        {
            _localCtx.Value = l;
    
        }
    }
    

    调用:

            ThreadLocalTest.Test1 t1 = new ThreadLocalTest.Test1();
            t1.setList(new List<string>() { "a", "b", "c" });
    
            Thread t = new Thread(() => {
                t1.setList(new List<string>() { "aa", "bb", "cc" });
                foreach (string a in t1.getList())
                {
                    Console.WriteLine("Thread:"+a);
                }
            });
            t.Start();
            t.Join();
            foreach (string a in t1.getList())
            {
                Console.WriteLine(a);
            }
    
    image.png

    例子2:

        static void Main(string[] args)
        {
            ConsoleApplication37.Test1 t1 = new ConsoleApplication37.Test1();
            List<string> l = t1.getList();
            l.Add("a");
            l.Add("b");
            l.Add("c");
            Thread t = new Thread(() =>
            {
                List<string> l2 = t1.getList();
                l2.Add("a2");
                l2.Add("b2");
                l2.Add("c2");
                foreach (string a in t1.getList())
                {
                    Console.WriteLine("Thread:" + a);
                }
            });
            t.Start();
            t.Join();
            foreach (string a in t1.getList())
            {
                Console.WriteLine(a);
            }
        }
    
    image.png

    用了ThreadLocal,哪怕是同一个对象,如果调用对象的是不同的线程,ThreadLocal也会new一个新的内部属性。

    相关文章

      网友评论

          本文标题:ThreadLocal

          本文链接:https://www.haomeiwen.com/subject/hjuhjftx.html