package onlyfun.caterpillar;
import java.util.*;
public class ThreadLocal {
// åå¾ä¸å忥åçMapç©ä»¶
private Map storage =
Collections.synchronizedMap(new HashMap());
public T get() {
// åå¾ç®åå·è¡get()æ¹æ³çå·è¡ç·
Thread current = Thread.currentThread();
// æ ¹æå·è¡ç·åå¾å·è¡ç·èªæçè³æº
T t = storage.get(current);
// 妿鿲æå·è¡ç·å°ç¨çè³æºç©ºé
// å建ç«ä¸åæ°ç空é
if(t == null &&
!storage.containsKey(current)) {
t = initialValue();
storage.put(current, t);
}
return t;
}
public void set(T t) {
storage.put(Thread.currentThread(), t);
}
public T initialValue() {
return null;
}
}