package com.susan.thread;
import java.util.HashMap;
import java.util.Random;
/**
* ThreadLocal类及其应用
*
* @author dahai
*
*/
public class ThreadLocalTest {
private static ThreadLocal
private static ThreadLocal
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int data = new Random().nextInt();
t.set(data);
System.out.println(Thread.currentThread().getName() + "线程已经准备好数据:" + data);
MyThreadScopeData mydata = new MyThreadScopeData();
mydata.setName("name:"+data);
mydata.setAge(data);
t1.set(mydata);
new A().get();
new B().get();
}
}).start();
}
}
static class A {
public void get() {
MyThreadScopeData mydata = t1.get();
System.out.println("A:" + mydata.getName()+"/////" + mydata.getAge());
}
}
static class B {
public void get() {
MyThreadScopeData mydata = t1.get();
System.out.println("B:" + mydata.getName()+"/////" + mydata.getAge());
}
}
}
class 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;
}
}