关于ThreadLocal的一个问题
public class ThreadLocalTest {public static void main(String[] args) {
MyRunnable r = new MyRunnable();
r.setMyLocal(100);
new Thread(r).start();
}
}
class MyRunnable implements Runnable {
private ThreadLocal<Integer> myLocal = new ThreadLocal<Integer>();
public void setMyLocal(int i) {
myLocal.set(i);
}
public void run() {
System.out.println(myLocal.get());
myLocal.set(200);
System.out.println(myLocal.get());
}
}
输出结果是:
null
200
为什么不是:
100
200 ??