Java线程和多线程(七)——ThreadLocal
Java中的ThreadLocal是用来创建线程本地变量用的。我们都知道,访问某个对象的所有线程都是能够共享对象的状态的,所以这个对象状态就不是线程安全的。开发者可以通过使用同步来保证线程安全,但是如果不希望使用同步的话,我们也可以使用ThreadLocal
变量。
Java ThreadLocal
其实每个线程都有自己的ThreadLocal
变量,并且这个变量可以通过get()
和set()
方法来获取默认值,或者修改其值。
ThreadLocal实例可以配置为静态私有变量来关联线程的状态。
Java ThreadLocal举例
下面的例子展示了在Java程序中如何使用ThreadLocal
,也同时证实了,线程中都保留一份ThreadLocal的拷贝的。
package com.sapphire.threads;
import java.text.SimpleDateFormat;
import java.util.Random;
public class ThreadLocalExample implements Runnable{
// SimpleDateFormat is not thread-safe, so give one to each thread
// SimpleDateFormat is not thread-safe, so give one to each thread
private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue()
{
return new SimpleDateFormat("yyyyMMdd HHmm");
}
};
public static void main(String[] args) throws InterruptedException {
ThreadLocalExample obj = new ThreadLocalExample();
for(int i=0 ; i<10; i++){
Thread t = new Thread(obj, ""+i);
Thread.sleep(new Random().nextInt(1000));
t.start();
}
}
@Override
public void run() {
System.out.println("Thread Name= "+Thread.currentThread().getName()+" default Formatter = "+formatter.get().toPattern());
try {
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
formatter.set(new SimpleDateFormat());
System.out.println("Thread Name= "+Thread.currentThread().getName()+" formatter = "+formatter.get().toPattern());
}
}
上面程序的输出结果类似下面的结果:
Thread Name= 0 default Formatter = yyyyMMdd HHmm
Thread Name= 1 default Formatter = yyyyMMdd HHmm
Thread Name= 0 formatter = M/d/yy h:mm a
Thread Name= 2 default Formatter = yyyyMMdd HHmm
Thread Name= 1 formatter = M/d/yy h:mm a
Thread Name= 3 default Formatter = yyyyMMdd HHmm
Thread Name= 4 default Formatter = yyyyMMdd HHmm
Thread Name= 4 formatter = M/d/yy h:mm a
Thread Name= 5 default Formatter = yyyyMMdd HHmm
Thread Name= 2 formatter = M/d/yy h:mm a
Thread Name= 3 formatter = M/d/yy h:mm a
Thread Name= 6 default Formatter = yyyyMMdd HHmm
Thread Name= 5 formatter = M/d/yy h:mm a
Thread Name= 6 formatter = M/d/yy h:mm a
Thread Name= 7 default Formatter = yyyyMMdd HHmm
Thread Name= 8 default Formatter = yyyyMMdd HHmm
Thread Name= 8 formatter = M/d/yy h:mm a
Thread Name= 7 formatter = M/d/yy h:mm a
Thread Name= 9 default Formatter = yyyyMMdd HHmm
Thread Name= 9 formatter = M/d/yy h:mm a
从代码中我们可以看到,10个线程都共享同一个对象,引用的是同一个ThreadLocal<SimpleDateFormat> formatter
,看上面的代码,当线程0执行了formatter.set(new SimpleDateFormat())
的时候,显然,读取的线程2的formatter仍然是默认的formatter,说明修改公共的formatter其实并没有生效,从每个线程单独来看,也没有破坏线程的安全性。
ThreadLocal原理
到了这里,很多人会奇怪,ThreadLocal
的实现方式,下面我们来看下ThreadLocal
的实现方案,首先看下这个其set和get方法的实现方案:
/**
* Returns the value in the current thread's copy of this
* thread-local variable. If the variable has no value for the
* current thread, it is first initialized to the value returned
* by an invocation of the {@link #initialValue} method.
*
* @return the current thread's value of this thread-local
*/
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
/**
* Sets the current thread's copy of this thread-local variable
* to the specified value. Most subclasses will have no need to
* override this method, relying solely on the {@link #initialValue}
* method to set the values of thread-locals.
*
* @param value the value to be stored in the current thread's copy of
* this thread-local.
*/
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
get和set方法中都有一个核心的概念,就是ThreadLocalMap
其实,这个Map是根据线程绑定的,参考如下代码:
/**
* Get the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
*
* @param t the current thread
* @return the map
*/
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
上面的代码是ThreadLocal
中的getMap(Thread t)
方法,这个方法来返回绑定到线程上的线程本地变量。线程的内部其实都会维护ThreadLocalMap
的。通过前面的set和get方法,那么我们就知道ThreadLocal
的实现方案了。ThreadLocalMap
本质上,是一个HashMap,从线程到类型T的一个映射。这也就解释了,为什么我们将ThreadLocal定义为static final
仍然不会影响线程的安全,因为我们之前代码中访问到的formatter其实都已经扔到了ThreadLocalMap里面,这样,每次调用get,其实会通过Thread.currentThread()
找到对应的ThreadLocalMap
,进而找到对应的formmater副本,调用set方法改变的都是ThreadLocalMap里面的值,自然就不会影响到我们在ThreadLocalExample
之中的formatter变量,自然也就不存在线程安全问题。
同时,这也解释了我们为什么ThreadLocal的变量定义为了static final
的,因为就算定义为非static的,仍然是没有任何意义的,只会增加额外的内存而已,因为我们本质上修改的不是ThreadLocalExample
中的实例,而是ThreadLocalMap
中的副本,所以定义为static final
正合适。
ThreadLocal
本质上其实是将一些变量副本写入Thread当中的,所以内存占用会更大,开发者可以根据自己的需求考虑是通过同步或者ThreadLocal
的方式来实现线程安全操作。
最新文章
- 解决UINavigationController在pushViewController时出现的";卡顿";问题
- win7+vs2008+windows mobile6.5.3
- 压缩文本、字节或者文件的压缩辅助类-GZipHelper 欢迎收藏
- excel列递增方法技巧
- UE用法
- php 上传图片
- Java科普之算法剖析
- UDP包的最大大小是多少?
- readonly和const区别
- mysql语句中使用like后面的%(百分号)的问题
- swipe和swiper的区别
- GDAL C#中文路径,中文属性名称乱码问题
- 字符转码开源库libiconv目前还不支持64位
- Fortran与C/C++混合编程示例
- Spring Cloud Config采用数据库存储配置内容
- qtp 自动化测试--点滴 菜单没有了,有些控件运行时找不到
- 离线安装Python包hickle,easydict
- 使用mutt自动发送邮件
- 20165234 预备作业3 Linux安装及学习
- 枚举类型enum详解——C语言
热门文章
- Angular JS + Express JS入门搭建网站
- Springboot中SpringMvc拦截器配置与应用(实战)
- java最大最小堆
- C# 对XML操作-实例
- Windows下hosts文件的作用
- web service 对外发布一个hello world接口(入门)
- struts2.3.4.1转换成eclipse项目的过程
- Js arguments.callee();函数自己调用自己
- ubuntu linux 使用命令行安装 google chrome
- dd-wrt ddns更新失败由于电信提供的ip不是公网ip