package cn.itcast.thread;
import java.util.HashMap;
import java.util.Map;
public class ThreadLocalTest {
/**
* @param args
*/
private static Map<Thread,Long> myThreadLoacl=new HashMap<Thread, Long>();
public static void main(String[] args) {
for(int i=0;i<2;i++){
new Thread(new Runnable(){
@Override
public void run() {
long data=System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+"has put data"+data);
new First().showData();
new Second().showData();
}}).start();
}
}
static class First{
public void showData(){
System.out.println(myThreadLoacl.isEmpty());
long data=myThreadLoacl.get(Thread.currentThread());//这里会发生空指针问题,但是上一行却打印的是true,怎么回事
System.out.println("first"+Thread.currentThread().getName()+" "+data);
}
}
static class Second{
public void showData(){
long data=myThreadLoacl.get(Thread.currentThread());
System.out.println("second"+Thread.currentThread().getName()+" "+data);
}
}
} |