美文网首页
ThreadLocal

ThreadLocal

作者: 寂静的春天1988 | 来源:发表于2020-08-31 16:37 被阅读0次

    https://www.imooc.com/learn/1217

    并发问题:多个线程去读取同一份共享的资源时,就会发生一致性问题。

    解决方法:
    1、加锁:避免并发访问资源
    2、使用ThreadLocal,这样每个线程都有自己单独的资源,避免共享资源。

    定义:ThreadLocal称之为线程的局部变量,每一个线程都有单独的副本。

    基本api
    ThreadLocal<String>()构造函数
    initialValue初始化值
    set/get设置值/获取值
    remove//删除值

            ThreadLocal<String> threadLocal=new ThreadLocal<String>() {
                @Override
                protected String initialValue() {
                    return "hello";
                }
            };
            threadLocal.set("123");
            System.out.println(threadLocal.get());
            threadLocal.remove();
            System.out.println(threadLocal.get());
    

    举个例子

    @RestController
    public class TestController {
        static Integer c = 0;
        
        @RequestMapping("/stat")
        public Integer stat() {
            return c;
        }
        @RequestMapping("/count")
        public void count() throws InterruptedException {
            add();
        }
        
        private void add() throws InterruptedException {
            Thread.sleep(100);
            c++;
        }
    }
    

    访问count接口每次将统计变量c加1,访问stat返回统计变量。很显然如果开100个线程访问10000次,这样多线程访问就会产生并发问题。


    image.png

    第一种方案:加锁

        private synchronized void  add() throws InterruptedException {
            Thread.sleep(100);
            c++;
        }
    

    将add方法锁起来,线程间访问add必须排队。这样就避免了同时去访问,但这样也极大的降低了效率。

    第二种方案:使用ThreadLocal

    @RestController
    public class TestController {
        
        
        static ThreadLocal<Integer> c=new ThreadLocal<Integer>() {
            @Override
            protected Integer initialValue() {
                return 0;
            };
        };
        
        
        @RequestMapping("/stat")
        public Integer stat() {
            return c.get();
        }
        @RequestMapping("/count")
        public void count() throws InterruptedException {
            add();
        }
        
        private  void  add() throws InterruptedException {
            Thread.sleep(100);
            c.set(c.get()+1);
        }
    }
    

    这样避免了多个线程共享一份资源,每个线程都有自己的副本。

    这2种方法虽然都可以解决并发安全,但实际上是2种思路。加锁是让线程必须排队来读取资源,降低了效率。而使用ThreadLocal是让线程间不要共享同一份资源。

    上面的代码还是有一点小问题。使用ThreadLocal返回的是每一个线程统计的,需要将所有线程统计的数据累加起来返回。

    @RestController
    public class TestController {
        
        static final HashSet<Val<Integer>> SET=new HashSet<Val<Integer>>();
        
        static ThreadLocal<Val<Integer>> C=new ThreadLocal<Val<Integer>>() {
            @Override
            protected Val<Integer> initialValue() {
                Val<Integer> v=new Val<Integer>();
                v.setV(0);
                addSet(v);
                return v;
            };
        };
        
        private static synchronized void addSet(Val<Integer> val) {
            SET.add(val);
        }
    
        @RequestMapping("/stat")
        public Integer stat() {
            return SET.stream().map(Val::getV).reduce(Integer::sum).orElse(-1);
        }
        @RequestMapping("/count")
        public void count() throws InterruptedException {
            Thread.sleep(100);
            Val<Integer> v=C.get();
            v.setV(v.getV()+1);
        }
        
    }
    
    

    由于hashSet的并不是线程安全的,所以还是在add方法上加了锁。但是缩小了锁的范围,提高了效率。

    源码分析

    现在基本看不太懂。。。

    相关文章

      网友评论

          本文标题:ThreadLocal

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