zoukankan      html  css  js  c++  java
  • JAVA多线程学习五:线程范围内共享变量&ThreadLocal

    一、概念

     可以将每个线程用到的数据与对应的线程号存放到一个map集合中,使用数据时从这个集合中根据线程号获取对应线程的数据,就可以实现线程范围内共享相同的变量。

    二、代码

    Runnable中的run()方法里面执行Thread.currentThread()都会对应当前Runnable对应的线程,因此A、B中对应的Thread.currentThread()对应所在的Runnable对应的线程

    复制代码
    public class ThreadScopeShareData {
    
        private static Map<Thread, Integer> threadData = new HashMap<Thread, Integer>();
        public static void main(String[] args) {
            for(int i=0;i<2;i++){
                new Thread(new Runnable(){
                    @Override
                    public void run() {
                        int data = new Random().nextInt();
                        System.out.println(Thread.currentThread().getName() 
                                + " has put data :" + data);
                        threadData.put(Thread.currentThread(), data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    
        static class A{
            public void get(){
                int data = threadData.get(Thread.currentThread());
                System.out.println("A from " + Thread.currentThread().getName() 
                        + " get data :" + data);
            }
        }
    
        static class B{
            public void get(){
                int data = threadData.get(Thread.currentThread());          
                System.out.println("B from " + Thread.currentThread().getName() 
                        + " get data :" + data);
            }       
        }
    }
    复制代码

    三、ThreadLocal

    JDK1.5提供了ThreadLocal类来方便实现线程范围内的数据共享,它的作用就相当于前面中的Map(内部并不是Map),也就是让每个线程拥有自己的值
    一个ThreadLocal对象只能记录一个线程内部的一个共享变量,需要记录多个共享数据,可以创建多个ThreadLocal对象,或者将这些数据进行封装,将封装后的数据对象存入ThreadLocal对象中。
    线程结束后也可以自动释放相关的ThreadLocal变量,也可以调用ThreadLocal.remove()方法用来更快释放内存。

    代码:

    复制代码
    public class ThreadLocalTest {  
    
        private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();  
        public static void main(String[] args) {  
    
            //启动两个线程  
            for (int i = 0; i < 2; i++) {  
                new Thread(new Runnable() {  
                    @Override  
                    public void run() {  
                        //创建每个线程私有的变量  
                        int data = new Random().nextInt(100);  
                        System.out.println(Thread.currentThread().getName()+" has put data: "+data);  
                        //往local里面设置值  
                        threadLocal.set(data);  
                        new A().get();  
                        new B().get();  
                    }  
                }).start();  
            }  
        }  
    
        static class A{  
            public void get(){  
                int data =threadLocal.get();  
                System.out.println("A from "+Thread.currentThread().getName()+" has get data: "+data);  
            }  
        }
    
        static class B{  
            public void get(){  
                int data =threadLocal.get();  
                System.out.println("B from "+Thread.currentThread().getName()+" has get data: "+data);  
            }  
        }  
    }  
    复制代码

    假设需要保存不止一个值,可以把其他属性的值打包成一个类,然后将该类设置成ThreadLocal的值。

    下面代码中,在类MyThreadLocalScopeDate里面定义了一个静态变量Map,用来保存所有线程创建的MyThreadLocalScopeDate,并使用单例使得不管多少线程都只创建一个MyThreadLocalScopeDate对象。

    复制代码
    public class ThreadLocalTest {  
    
        private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();  
        public static void main(String[] args) {  
    
            //启动两个线程  
            for (int i = 0; i < 2; i++) {  
                new Thread(new Runnable() {  
                    @Override  
                    public void run() {  
                        //创建每个线程私有的变量  
                        int data = new Random().nextInt(100);  
                        System.out.println(Thread.currentThread().getName()+" has put data: "+data);  
                        //往local里面设置值  
                        threadLocal.set(data);  
                        //获取自己线程的MyThreadLocalScopeDate实例对象  
                        MyThreadLocalScopeDate myData = MyThreadLocalScopeDate.getThreadInstance();  
                        myData.setName("name"+data);  
                        myData.setAge(data);  
                        new A().get();  
                        new B().get();  
                    }  
                }).start();  
            }  
        }  
    
        static class A{  
            public void get(){  
                int data =threadLocal.get();  
                System.out.println("A from "+Thread.currentThread().getName()+" has get data: "+data);  
                MyThreadLocalScopeDate myData = MyThreadLocalScopeDate.getThreadInstance();  
                System.out.println("A from "+Thread.currentThread().getName()+" has get MyThreadLocalScopeDate name: "+myData.getName()+" , age: "+myData.getAge());  
            }  
        }  
    
    
        static class B{  
            public void get(){  
                int data =threadLocal.get();  
                System.out.println("B from "+Thread.currentThread().getName()+" has get data: "+data);  
                MyThreadLocalScopeDate myData = MyThreadLocalScopeDate.getThreadInstance();  
                System.out.println("B from "+Thread.currentThread().getName()+" has get MyThreadLocalScopeDate name: "+myData.getName()+" , age: "+myData.getAge());  
            }  
        }  
    }  
    
    class MyThreadLocalScopeDate{//单例模式  
    
        private MyThreadLocalScopeDate(){};//构造方法私有化  
        private static ThreadLocal<MyThreadLocalScopeDate> map = new ThreadLocal<MyThreadLocalScopeDate>();//封装MyThreadLocalScopeDate是线程实现范围内共享  
    
        //思考AB两个线程过来的情况 自己分析 AB都需要的自己的对象 没有关系 所以不需要同步 如果有关系就需要同步了  
        public static /*synchronized*/MyThreadLocalScopeDate getThreadInstance(){  
            MyThreadLocalScopeDate instance =map.get();  
            if(instance==null){  
                instance = new MyThreadLocalScopeDate();  
                map.set(instance);  
            }  
            return instance;  
        }  
    
        private String name;  
        private int age;  
        public String getName() {  
            return name;  
        }  
        public void setName(String name) {  
            this.name = name;  
        }  
        public int getAge() {  
            return age;  
        }  
        public void setAge(int age) {  
            this.age = age;  
        }  
    }
    复制代码

    出处:https://www.cnblogs.com/pony1223/p/9251641.html

  • 相关阅读:
    UDP案例_在线咨询
    MFC对话框水平和垂直滚动条功能
    对话框中滚动条
    ON_COMMAND_RANGE 多个按钮响应一个函数
    char**赋值
    MFC如何使dialog对话框置顶
    如何让CListBox控件滚动条自动向下滚动?
    不带,以及带参数,带返回值的Lambda表达式
    JAVA学习_多线程技术
    最烦有些技术帖上来就放代码
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/13341852.html
Copyright © 2011-2022 走看看