ThreadLocal实现线程范围的共享变量

线程范围内共享数据的示意图



ThreadLocal的作用和目的: 用于实现线程内的数据共享, 即对于相同的程序代码, 多个模块在同一线程中运行时要共享一份数据, 而在另外线程中运行时又共享另外一份数据
每个线程调用全局ThreadLocal对象的set方法, 就相当于往其内部的map中增加一条记录, key分别是各自的线程, value是各自的set方法传递进去的值. 在线程结束时可以调用ThreadLocal.clear()方法, 这样会更快释放内存, 不调用也可以, 因为线程结束后也可以自动释放相关的ThreadLocal变量

ThreadLocal的应用场景

订单处理包含一系列操作: 减少库存量, 增加一条流水台账, 修改总账, 这几个操作要在同一个事务中完成, 通常也是在同一个线程中进行处理, 如果公司应收款的操作失败了, 则应该把前面的操作回滚, 否则, 提交所有操作, 这要求这些操作使用相同的数据库连接对象, 而这些操作的代码分别位于不同的模块类中

Struts2的ActionContext, 同一段代码被不同的线程调用运行时, 该代码操作的数据是每个线程各自的状态和数据, 对于不同的线程来说, getContext()方法拿到的对象都不相同, 读同一个线程来说, 不管调用getContext()方法多少次和在哪个模块中getContext()方法, 拿到的都是同一个

举例说明 创建三个线程, 他们都访问了三个对象, 第一个对象设置值, 第二三个对象取值, 同一个线程设置的值, 只能被相同的线程获取


实现对ThreadLocal变量的封装, 让外界不要直接操作ThreadLocal变量
对基本类型的数据的封装, 这种应用相对很少见
对对象类型的数据的封装, 比较常见, 即让某个类针对不同线程分别创建一个独立的实例对象

总结: 一个ThreadLocal代表一个变量, 故其中只能放一种数据, 你有两个变量都要线程范围内共享, 则要定义两个ThreadLocal对象, 如果有一百个变量要线程共享呢? 那请定义一个对象来封装, 然后在ThreadLocal中存储这一个对象

代码

package thread;

import java.util.Random;

public class ThreadLocalTest {

	public static void main(String[] args) {
		for (int i = 0; i < 2; i++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					MyThreadScopeData threadInstance = MyThreadScopeData.getThreadInstance();
					String name = new Random().nextInt() + "";
					int age = new Random().nextInt();
					threadInstance.setName(name);
					threadInstance.setAge(age);
					System.out.println(Thread.currentThread().getName() + " has put name : " + name);
					System.out.println(Thread.currentThread().getName() + " has put age : " + age);
					new A().get();
					new B().get();
				}
			}, "thread " + i).start();
		}
	}

	// A B 两个模块
	static class A {
		public void get() {
			MyThreadScopeData threadInstance = MyThreadScopeData.getThreadInstance();
			System.out.println("A from " + Thread.currentThread().getName() + " get name : " + threadInstance.getName());
			System.out.println("A from " + Thread.currentThread().getName() + " get age : " + threadInstance.getAge());
		}
	}

	static class B {
		public void get() {
			MyThreadScopeData threadInstance = MyThreadScopeData.getThreadInstance();
			System.out.println("B from " + Thread.currentThread().getName() + " get name : " + threadInstance.getName());
			System.out.println("B from " + Thread.currentThread().getName() + " get age : " + threadInstance.getAge());
		}
	}
}

class MyThreadScopeData {// 线程范围内共享类 把线程内需要共享的变量 通通封装到这个类中 然后在用ThreadLocal 封装起来

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

	// 不允许直接创建该对象
	private MyThreadScopeData() {
	}

	// 提供静态方法 返回和线程相关的实例数据对象
	public static MyThreadScopeData getThreadInstance() {
		// 这样外部就看不到ThreadLocal对象了
		MyThreadScopeData instance = map.get();
		if (instance == null) {
			instance = new MyThreadScopeData();
			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;
	}

}

发布了31 篇原创文章 · 获赞 10 · 访问量 9399
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章