多线程 05 线程范围内共享变量的概念与作用

 ThreadLocal类及应用技巧

(1)非线程内的数据共享:

public class ThreadScopeShareData {

    private static int shareData;

    public static void main(String[] args){

        for(int i=0;i<2;i++){

            new Thread(new Runnable() {

                @Override

                public void run() {

                     shareData= new Random().nextInt();

                    System.out.println(Thread.currentThread().getName()+"has put "+shareData);

                    new A().get();

                    new B().get();

                }

            }).start();

        }

    }

    static class A{

        public void get(){

            System.out.println("A from "+Thread.currentThread().getName()+"  get data "+shareData);

        }

    }

    static class B{

        public void get(){

            System.out.println("B from "+Thread.currentThread().getName()+"  get data "+shareData);

        }

    }

}

(2)自己实现线程内的共享:

/**

 * 实现线程内的共享数据

 */

public class ThreadScopeShareData {

    private static  Integer data=0;

    private static ReentrantLock reentrantLock = new ReentrantLock();

    private static    Map<Thread,Integer> map=new HashMap<>();

    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();

                     map.put(Thread.currentThread(),data);

                    System.out.println(Thread.currentThread().getName()+" has put "+data);

                    new A().get();

                    new B().get();

                }

            }).start();

        }

    }

    static class A{

        public void get(){

             Integer data = map.get(Thread.currentThread());

            System.out.println("A from "+Thread.currentThread().getName()+"  get data "+data);

        }

    }

    static class B{

        public void get(){

            Integer data = map.get(Thread.currentThread());

            System.out.println("B from "+Thread.currentThread().getName()+"  get data "+data);

        }

}

//注意代码是有问题的,会出现重排序的问题:

Thread-0 has put 845819596

Thread-1 has put 1724703386

A from Thread-0  get data null

A from Thread-1  get data 1724703386

B from Thread-0  get data null

B from Thread-1  get data 1724703386

//更改一下:更改顺序,就不会有问题了。

     int data= new Random().nextInt();

                    System.out.println(Thread.currentThread().getName()+" has put "+data);

                    map.put(Thread.currentThread(),data);

                    new A().get();

                    new B().get();

                }

            }).start();

实际应用,比如线程池。

(3)工具实现线程内数据的共享:

public class ThreadLocalTest {

    private static ThreadLocal<Integer> threadLocal=new ThreadLocal();

    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);

                    threadLocal.set(data);

                    new A().get();

                    new B().get();

                }

            }).start();

        }

    }

    static class A{

        public void get(){

            Integer data = threadLocal.get();

            System.out.println("A from "+Thread.currentThread().getName()+"  get data "+data);

        }

    }

    static class B{

        public void get(){

            Integer data = threadLocal.get();

            System.out.println("B from "+Thread.currentThread().getName()+"  get data "+data);

        }

    }

}

(4)多个值的线程内数据的共享

/**

 *实体线程内数据共享

 */

public class ThreadLocalInstance {

    private static ThreadLocal<MyThreadScopeData> threadLocal=new ThreadLocal<>();

    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);

                    MyThreadScopeData myThreadScopeData = new MyThreadScopeData();

                    myThreadScopeData.setName(String.valueOf(data));

                    myThreadScopeData.setAge(data);

                    threadLocal.set(myThreadScopeData);

                    new A().get();

                    new B().get();

                }

            }).start();

        }

    }

    static class A{

        public void get(){

            MyThreadScopeData myThreadScopeData = threadLocal.get();

            System.out.println("A from "+Thread.currentThread().getName()+"  get name "+ myThreadScopeData.getName());

            System.out.println("A from "+Thread.currentThread().getName()+"  get age "+ myThreadScopeData.getAge());

        }

    }

    static class B{

        public void get(){

            MyThreadScopeData myThreadScopeData = threadLocal.get();

            System.out.println("B from "+Thread.currentThread().getName()+"  get name "+ myThreadScopeData.getName());

            System.out.println("B from "+Thread.currentThread().getName()+"  get age "+ myThreadScopeData.getAge());

        }

    }

    static class MyThreadScopeData{

        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;

        }

    }

}

(5)多个值的线程内数据共享的优化

package com.renrenche.thread;

 

import java.util.Random;

 

/**

 *实体线程内数据共享

 */

public class ThreadLocalInstance {

    private static ThreadLocal<MyThreadScopeData> threadLocal=new ThreadLocal<>();

    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);

                    MyThreadScopeData myThreadScopeData = MyThreadScopeData.getMyThreadScopeInstatnce();

                    myThreadScopeData.setName(String.valueOf(data));

                    myThreadScopeData.setAge(data);

                    threadLocal.set(myThreadScopeData);

                    new A().get();

                    new B().get();

                }

            }).start();

        }

    }

    static class A{

        public void get(){

            MyThreadScopeData myThreadScopeData = MyThreadScopeData.getMyThreadScopeInstatnce();

            System.out.println("A from "+Thread.currentThread().getName()+"  get name "+ myThreadScopeData.getName());

            System.out.println("A from "+Thread.currentThread().getName()+"  get age "+ myThreadScopeData.getAge());

        }

    }

    static class B{

        public void get(){

            MyThreadScopeData myThreadScopeData = MyThreadScopeData.getMyThreadScopeInstatnce();

            System.out.println("B from "+Thread.currentThread().getName()+"  get name "+ myThreadScopeData.getName());

            System.out.println("B from "+Thread.currentThread().getName()+"  get age "+ myThreadScopeData.getAge());

        }

    }

    static class MyThreadScopeData{

        private static ThreadLocal<MyThreadScopeData> threadLocal=new ThreadLocal<>();

        private MyThreadScopeData(){

        }

        public static synchronized MyThreadScopeData getMyThreadScopeInstatnce(){

            MyThreadScopeData myThreadScopeData = threadLocal.get();

            if(myThreadScopeData==null){

                myThreadScopeData=new MyThreadScopeData();

                threadLocal.set(myThreadScopeData);

            }

            return  myThreadScopeData;

        }

        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;

        }

    }

}

如有疑问,请发邮件:1176306419@qq.com


github:  https://github.com/wangrui0/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值