1.不使用ThreadLocal,自己定义一个map,用来存放当前线程的数据
package com.norelax.www;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
public class ThreadScopeDataShare {
//不使用ThreadLocal,自己定义一个map,用来存放当前线程的数据
public static Map<Thread, Integer> threadData=new ConcurrentHashMap<Thread, Integer>();
public static void main(String[] args) {
//开启两个线程,分别向threadData放数据
for (int i = 1; i <=2; i++) {
new Thread(new Runnable() {
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().getData();
new B().getData();
}
}).start();
}
}
static class A {
public void getData() {
Integer data = threadData.get(Thread.currentThread());
System.out.println("A from" + Thread.currentThread().getName() + "get data :"
+ data);
}
}
static class B {
public void getData() {
Integer data = threadData.get(Thread.currentThread());
System.out.println("B from" + Thread.currentThread().getName() + "get data :"
+ data);
}
}
}
特别要注意,这里面的
class A和
class B都是static类型的,不然在编译时出现
No enclosing instance of type ThreadScopeDataShare is accessible. Must qualify the allocation with an enclosing instance of type ThreadScopeDataShare (e.g. x.new A()
where x is an instance of ThreadScopeDataShare).
where x is an instance of ThreadScopeDataShare).
因为类A和类B都是在static方法中被调用
效果如下:
效果如下:
Thread-1has put data:1766462033
Thread-0has put data:1524868127
A fromThread-0get data :1524868127
A fromThread-1get data :1766462033
B fromThread-0get data :1524868127
B fromThread-1get data :1766462033
Thread-0has put data:1524868127
A fromThread-0get data :1524868127
A fromThread-1get data :1766462033
B fromThread-0get data :1524868127
B fromThread-1get data :1766462033
2.使用ThreadLocal保证线程范围内的数据共享
简单粗暴,直接上代码:
package com.norelax.www;
import java.util.Random;
import com.norelax.www.ThreadScopeDataShare.A;
import com.norelax.www.ThreadScopeDataShare.B;
public class ThreadLocalTest {
private static ThreadLocal<Integer> shareData = new ThreadLocal<Integer>();
public static void main(String[] args) {
for (int i = 1; i <= 2; i++) {
new Thread(new Runnable() {
public void run() {
int data = new Random().nextInt();
System.out.println(Thread.currentThread().getName()
+ "has put data:" + data);
//输入数据
shareData.set(data);
//获取数据
new A().getData();
new B().getData();
}
}).start();
}
}
static class A {
public void getData() {
Integer data = shareData.get();
System.out.println("A from" + Thread.currentThread().getName()
+ "get data :" + data);
}
}
static class B {
public void getData() {
Integer data = shareData.get();
System.out.println("B from" + Thread.currentThread().getName()
+ "get data :" + data);
}
}
}
结果如下:
Thread-1has put data:-2027879383
Thread-0has put data:1157262527
A fromThread-0get data :1157262527
A fromThread-1get data :-2027879383
B fromThread-0get data :1157262527
B fromThread-1get data :-2027879383
Thread-0has put data:1157262527
A fromThread-0get data :1157262527
A fromThread-1get data :-2027879383
B fromThread-0get data :1157262527
B fromThread-1get data :-2027879383
3.如果共享的数据是对象呢?
package com.norelax.www;
import java.util.Random;
public class ThreadLocalObjectTest {
public static void main(String[] args) {
for (int i = 1; i <=2; i++) {
new Thread(new Runnable() {
public void run() {
int data = new Random().nextInt();
System.out.println(Thread.currentThread().getName()
+ " has put age :" + data+"has put name:norelax"+data);
MyThreadScopeShareDate myThreadInstance = MyThreadScopeShareDate.getMyThreadInstance();
myThreadInstance.setAge(data);
myThreadInstance.setName("norelax"+data);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public void get(){
MyThreadScopeShareDate myData = MyThreadScopeShareDate.getMyThreadInstance();
System.out.println("A from " + Thread.currentThread().getName()
+ " getMyData: " + myData.getName() + "," +
myData.getAge());
}
}
static class B{
public void get(){
MyThreadScopeShareDate myData = MyThreadScopeShareDate.getMyThreadInstance();
System.out.println("B from " + Thread.currentThread().getName()
+ " getMyData: " + myData.getName() + "," +
myData.getAge());
}
}
}
//要共享的数据对象
class MyThreadScopeShareDate{
private MyThreadScopeShareDate() {};
private static ThreadLocal<MyThreadScopeShareDate> myThreadScopeShareDate=new ThreadLocal<MyThreadScopeShareDate>();
<span style="color:#ff0000;">//单利模式获取对应实体
public static MyThreadScopeShareDate getMyThreadInstance(){
MyThreadScopeShareDate myThreadScopeShare = myThreadScopeShareDate.get();
if (myThreadScopeShare==null) {
myThreadScopeShare=new MyThreadScopeShareDate();
myThreadScopeShareDate.set(myThreadScopeShare);
}
return myThreadScopeShare;
}</span>
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
结果如下:
Thread-1 has put age :-1848631193has put name:norelax-1848631193
Thread-0 has put age :542514109has put name:norelax542514109
A from Thread-0 getMyData: norelax542514109,542514109
A from Thread-1 getMyData: norelax-1848631193,-1848631193
B from Thread-1 getMyData: norelax-1848631193,-1848631193
B from Thread-0 getMyData: norelax542514109,542514109
Thread-0 has put age :542514109has put name:norelax542514109
A from Thread-0 getMyData: norelax542514109,542514109
A from Thread-1 getMyData: norelax-1848631193,-1848631193
B from Thread-1 getMyData: norelax-1848631193,-1848631193
B from Thread-0 getMyData: norelax542514109,542514109