传统线程变量--ThreadLocal解析

在程序的执行过程中为了提高程序的并发以及运行效率,往往会用到线程变量(不同线程获取到变量的值不一样,相互隔离;同一线程不同方法访问的变量是同一个)。

现在我观察ThreadLocal类的源代码进行加以解析,并通过简单的方法实现类似ThreadLocal的类;

ThreadLocal类接口很简单,只有4个方法,我们先来了解一下:

  • void set(Object value)设置当前线程的线程局部变量的值。
  • public Object get()该方法返回当前线程所对应的线程局部变量。
  • public void remove()将当前线程局部变量的值删除,当线程结束后,对应该线程的局部变量将自动被垃圾回收,所以显式调用该方法清除线程的局部变量并不是必须的操作,但它可以加快内存回收的速度。
  • protected Object initialValue()返回该线程局部变量的初始值,该方法是一个protected的方法,显然是为了让子类覆盖而设计的。这个方法是一个延迟调用方法,在线程第1次调用get()或set(Object)时才执行,并且仅执行1次。ThreadLocal中的缺省实现直接返回一个null。

根据这几个方法我们可以看出ThreadLocal其实比较像Map,根据Key来获取Value,只不过ThreadLocal的Key是自己自动生成的而已;

下面我们用Map来简单的实现一下:

public class Main{

	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);//把当前线程作为Key
					new MapA().get();
					new MapB().get();
				}
			}).start();
		}
	}
	
	static class MapA{
		public void get(){
			int data = threadData.get(Thread.currentThread());
			System.out.println("MapA from " + Thread.currentThread().getName() 
					+ " get data :" + data);
		}
	}
	
	static class MapB{
		public void get(){
			int data = threadData.get(Thread.currentThread());			
			System.out.println("MapB from " + Thread.currentThread().getName() 
					+ " get data :" + data);
		}		
	}
}

上述代码中,我们看到了Map可以实现同一线程获取的线程范围内的变量是相同的,而不同线程所获取到的变量不同;

下面使用ThreadLocal来实现线程范围内共享变量:

public class Main{
	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.getThreadInstance().setName("name" + data);
					MyThreadScopeData.getThreadInstance().setAge(data);
					new TLA().get();
					new TLB().get();
				}
			}).start();
		}
	}
	
	static class TLA{
		public void get(){
			int data = x.get();
			System.out.println("TLA from " + Thread.currentThread().getName() 
					+ " get data :" + data);
			MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();
			System.out.println("TLA from " + Thread.currentThread().getName() 
					+ " getMyData: " + myData.getName() + "," +
					myData.getAge());
		}
	}
	
	static class TLB{
		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());			
		}		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值