多线程(2)-ThreadLocal线程范围内的共享数据

本文深入探讨ThreadLocal的概念,它允许在线程范围内实现数据共享,确保线程间数据独立且不互相干扰。通过实例代码展示ThreadLocal如何替代传统Map实现线程局部变量,以及其在多线程环境中避免同步的关键作用。运行结果显示,ThreadLocal能有效地实现对象在多个线程间的独立存储,线程结束后,相关数据会被自动清理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要介绍下ThreadLocal,重要的概念是线程范围内的数据共享

线程之间的数据不相互干扰,int data = 0;A线程取A线程的数据data,B线程取B线程的数据data。怎样才能使上例子完成?我们直接上例子吧

public class ThreadScopeShareData {

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

定义map,key是线程名,value值是随机数,所以此时不同线程根据自己的key(即线程名)就能取到对应线程自己的数据;

这就是ThreadLocal的思想,而且每个线程之间开启自己的事务,数据在线程的范围内共享,在线程之间是相互独立的。

那我们换成ThreadLocal实现下,上代码

public class ThreadLocalTest {

	private static ThreadLocal<Integer> x = 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();
					System.out.println(Thread.currentThread().getName() 
							+ " has put data :" + data);
					x.set(data);
					
					new A().get();
					new B().get();
				}
			}).start();
		}
	}
	
	static class A{
		public void get(){
			int data = x.get();
			System.out.println("A from " + Thread.currentThread().getName() 
					+ " get data :" + data);
		
			System.out.println("A from " + Thread.currentThread().getName());
		}
	}
	
	static class B{
		public void get(){
			int data = x.get();			
			System.out.println("B from " + Thread.currentThread().getName() 
					+ " get data :" + data);
			
			System.out.println("B from " + Thread.currentThread().getName());			
		}		
	}
}

就是用ThreadLocal代替了map;

可以发现ThreadLocal<T>,是泛型,所以可以用对象包含多个数据,就可以实现多个数据在线程范围内的共享

上代码

public class ThreadLocalTest {

	private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
	private static ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();
	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);
					x.set(data);
/*					MyThreadScopeData myData = new MyThreadScopeData();
					myData.setName("name" + data);
					myData.setAge(data);
					myThreadScopeData.set(myData);*/
					MyThreadScopeData.getThreadInstance().setName("name" + data);
					MyThreadScopeData.getThreadInstance().setAge(data);
					new A().get();
					new B().get();
				}
			}).start();
		}
	}
	
	static class A{
		public void get(){
			int data = x.get();
			System.out.println("A from " + Thread.currentThread().getName() 
					+ " get data :" + data);
/*			MyThreadScopeData myData = myThreadScopeData.get();;
			System.out.println("A from " + Thread.currentThread().getName()
					+ " getMyData: " + myData.getName() + "," +
					myData.getAge());*/
			MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();
			System.out.println("A from " + Thread.currentThread().getName()
					+ " getMyData: " + myData.getName() + "," +
					myData.getAge());
		}
	}
	
	static class B{
		public void get(){
			int data = x.get();			
			System.out.println("B from " + Thread.currentThread().getName() 
					+ " get data :" + data);
			MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();
			System.out.println("B from " + Thread.currentThread().getName()
					+ " getMyData: " + myData.getName() + "," +
					myData.getAge());
		}		
	}
}

class MyThreadScopeData{
	private MyThreadScopeData(){}
	public static /*synchronized*/ MyThreadScopeData getThreadInstance(){
		MyThreadScopeData instance = map.get();
		if(instance == null){
			instance = new MyThreadScopeData();
			map.set(instance);
		}
		return instance;
	}
	//private static MyThreadScopeData instance = null;//new MyThreadScopeData();
	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;
	}
}

定义了数据共享对象MyThreadScopeData,单例饿汉式,把ThreadLocal定义在共享数据的内部。

注意观察这个MyThreadScopeData中的ThreadLocal对象map,因为是ThreadLocal类,所以不需要用synchronized关键字。

这样就实现了多个数据的线程级别范围内的共享。运行结果为

A from Thread-0 get data :-1802810102
A from Thread-1 get data :208977903
A from Thread-0 getMyData: name-1802810102,-1802810102
A from Thread-1 getMyData: name208977903,208977903
B from Thread-0 get data :-1802810102
B from Thread-1 get data :208977903
B from Thread-1 getMyData: name208977903,208977903
B from Thread-0 getMyData: name-1802810102,-1802810102

亲测可用。当某线程结束时,其间的数据会被垃圾回收。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值