美文网首页
ThreadLocal和本地变量区别

ThreadLocal和本地变量区别

作者: 阡陌笙箫 | 来源:发表于2020-05-18 15:26 被阅读0次

    总结:

    1. Static声明的ThreadLocal修饰的变量,为线程私有,即某个线程修改之后对其他线程没有影响
    2. Static声明的基本类型变量,为全局共享,即任意一个线程修改之后,对其他线程是可见的,记得加volatile

    如果在ThreadLocal变量前加volatile,不会生效

    public class ThreadLocalTest {
            //声明一个Threadlocal变量,包装的是Boolean类型,初始化false
        static ThreadLocal<Boolean> local = new ThreadLocal<Boolean>(){ 
            @Override
            protected Boolean initialValue() {
                return false;
            }
        };
    
        private static ThreadLocalTest instance;
        //双重
        public static ThreadLocalTest getInstance(){
            if(instance==null){
                synchronized (ThreadLocalTest.class) {
                    if(instance==null){
                        instance = new ThreadLocalTest();
                    }
                }
            }
            return instance;
        }
        
        public void set(boolean value){
            local.set(value);
        }
        
        public boolean get(){
            return local.get();
        }
    
        public static void main(String[] args) {
            System.out.println(Thread.currentThread().getName()+":"+ThreadLocalTest.getInstance().get());//false
            ThreadLocalTest.getInstance().set(true);
            System.out.println(Thread.currentThread().getName()+":"+ThreadLocalTest.getInstance().get());//true
            
            new Thread(new Runnable() {
                public void run() {
                    //ThreadLocalTest.getInstance().set(true);
                    System.out.println(Thread.currentThread().getName()+":"+ThreadLocalTest.getInstance().get()); //true
                }
            }).start();
            
            new Thread(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName()+":"+ThreadLocalTest.getInstance().get());//false
                }
            }).start();
        }
    }
    
    
    public class ThreadLocalTest2 {
        
        static Boolean flag  = false; //将threadlocal变量换成普通类型变量
        
        private static ThreadLocalTest2 instance;
        
        public static ThreadLocalTest2 getInstance(){
            if(instance==null){
                synchronized (ThreadLocalTest2.class) {
                    if(instance==null){
                        instance = new ThreadLocalTest2();
                    }
                }
            }
            return instance;
        }
        
        public void set(boolean value){
            flag = value;
        }
        
        public boolean get(){
            return flag;
        }
    
        public static void main(String[] args) {
                    //第一次为false,因为默认是false
            System.out.println(Thread.currentThread().getName()+":"+ThreadLocalTest2.getInstance().get());//false
            
            //设置一次之后,全局共享
            ThreadLocalTest2.getInstance().set(true);
            System.out.println(Thread.currentThread().getName()+":"+ThreadLocalTest2.getInstance().get());//true
            
                    //该变量对其他线程都是共享的
            new Thread(new Runnable() {
                public void run() {
                    //ThreadLocalTest2.getInstance().set(true); //如果将赋值语句放在此处,将上面的set注释掉,运行几次看看结果是否还一样
                    System.out.println(Thread.currentThread().getName()+":"+ThreadLocalTest2.getInstance().get()); //true
                }
            }).start();
            
            new Thread(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName()+":"+ThreadLocalTest2.getInstance().get());//true
                }
            }).start();
        }
    }
    

    相关文章

      网友评论

          本文标题:ThreadLocal和本地变量区别

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