线程范围ThreadLocal

线程范围内的共享变量,通俗的说就是指:特定的线程对应特定的数据,不会因为对象的变化数据而改变。

ThreadLocal 的使用方便我们对不同的线程管理不同的数据,而且能够很好的对单例进行复用,因为我们通常不同的数据对象对应不同的单例进行保存,如果一旦分类数据过多,那么我们得创建大量的单例进行保存。然而ThreaLocal能够做到单例的复用。下面请看代码

public class ThreadScopShareData {
	
	
	 static ThreadLocal<Integer> x= new ThreadLocal<Integer>();
	  
//	private static  int data = 0;
//	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() {
					// TODO Auto-generated method stub
					int  data = new Random().nextInt();
					System.out.println(Thread.currentThread().getName()+"has put data: "+data);
					x.set(data);//数据存入到当前线程中
//					threadData.put(Thread.currentThread(),data);
					MyThreadScopeData.getInstance().setName("name"+data);//本线程对应的实例
					MyThreadScopeData.getInstance().setAge(data);
					new A().get();
					new B().get();
				}
			}).start();
			
		}
		
	
	}
	
	static  class A{
		
		public int get(){
			
//			int data = threadData.get(Thread.currentThread());
			int data = x.get();//获取当前线程数据
			System.out.println("A from"+Thread.currentThread().getName()+"get data: "+data);
			
			MyThreadScopeData mydata =  MyThreadScopeData.getInstance();
			
			System.out.println("A from"+Thread.currentThread().getName()+"get Mydata: "+mydata.getAge()+","+mydata.getName());
			return data;
		}
	}
     static  class B{
		
		public int get(){
			int data = x.get();//获取当前线程数据
//			int data = threadData.get(Thread.currentThread());
			System.out.println("B from"+Thread.currentThread().getName()+"get data: "+data);
			

			MyThreadScopeData mydata =  MyThreadScopeData.getInstance();
			
			System.out.println("B from"+Thread.currentThread().getName()+"get Mydata: "+mydata.getAge()+","+mydata.getName());
			return data;
		}
	}
	

}


class MyThreadScopeData{
	
	
//	private static MyThreadScopeData instance=null;
	
	private MyThreadScopeData(){
		
	}
	
	public static /*synchronized*/ MyThreadScopeData  getInstance(){//加同步锁是为了防止出现多个对象
		
		
		MyThreadScopeData instance = map.get();//此处可以针对不同线程new 对应的单例,提高代码的复用
		if(instance ==null){
			instance = new MyThreadScopeData();
			map.set(instance);
		}
		
		return instance;
		
	}
	
	
	private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<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;
	}
}

该代码的运行结果为


可见对象的不同,只要是线程一样数据就一样。

这对于我们在处理并发访问上给出了一些提示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖摸鱼专业户

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值