import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public
class ThreadLocalTest {
private static Map<Thread, Integer>
threadData = new HashMap<Thread, Integer>();
private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
/**
*
开启两个
线程,
* 第一个线程数据共享
* 第二个线程数据共享
* @param args
*/
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);
threadLocal.set(data);
MyThreadScopeData.getInstance().setName("name"+data);
MyThreadScopeData.getInstance().setAge(data);
new A().get();
new B().get();
}
}).start();
}
}
static class A {
public void get() {
int data = threadData.get(Thread.currentThread());
System.out.println("A1 " + Thread.currentThread().getName()
+ " has put data :" + data);
int data2 = threadLocal.get();
System.out.println("A2 " + Thread.currentThread().getName()
+ " has put data :" + data2);
MyThreadScopeData myThreadScopeData = MyThreadScopeData.getInstance();
System.out.println("A3 " + Thread.currentThread().getName()
+ " name :" + myThreadScopeData.getName() +" , age :" + myThreadScopeData.getAge() );
}
}
static class B {
public void get() {
int data = threadData.get(Thread.currentThread());
System.out.println("B " + Thread.currentThread().getName()
+ " has put data :" + data);
int data2 = threadLocal.get();
System.out.println("B2 " + Thread.currentThread().getName()
+ " has put data :" + data2);
MyThreadScopeData myThreadScopeData = MyThreadScopeData.getInstance();
System.out.println("B3 " + Thread.currentThread().getName()
+ " name :" + myThreadScopeData.getName() +" , age :" + myThreadScopeData.getAge() );
}
}
}
class MyThreadScopeData{
/*//饱汉模式
private MyThreadScopeData(){}
public static MyThreadScopeData getInstance(){
return myThreadScopeData;
}
private static MyThreadScopeData myThreadScopeData= new MyThreadScopeData();*/
/*//恶汉模式
private MyThreadScopeData(){}
public static synchronized MyThreadScopeData getInstance(){
if(myThreadScopeData==null)
myThreadScopeData = new MyThreadScopeData();
return myThreadScopeData;
}
private static MyThreadScopeData myThreadScopeData = null;*/
private MyThreadScopeData(){}
public static synchronized MyThreadScopeData getInstance(){
MyThreadScopeData
instance = myThreadScopeData.get();
if(instance==null){
instance = new MyThreadScopeData();
myThreadScopeData.set(instance);
}
return instance;
}
private static ThreadLocal<MyThreadScopeData> myThreadScopeData = 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;
}
}
运行结果:
Thread-0 has put data :-412573948
Thread-1 has put data :-589943191
A1 Thread-1 has put data :-589943191
A1 Thread-0 has put data :-412573948
A2 Thread-1 has put data :-589943191
A2 Thread-0 has put data :-412573948
A3 Thread-1 name :name-589943191 , age :-589943191
A3 Thread-0 name :name-412573948 , age :-412573948
B Thread-1 has put data :-589943191
B Thread-0 has put data :-412573948
B2 Thread-1 has put data :-589943191
B2 Thread-0 has put data :-412573948
B3 Thread-1 name :name-589943191 , age :-589943191
B3 Thread-0 name :name-412573948 , age :-412573948