本文主要介绍下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
亲测可用。当某线程结束时,其间的数据会被垃圾回收。