调用者:
private static ThreadLocal s_doneCode = new ThreadLocal();
public static long getDoneCode() {
Long done_code = (Long)s_doneCode.get();
if (done_code == null) {
long doneCode = 100000000000000L;
doneCode = doneCode + ServiceManager.getDoneCode();//这里是取的getNewId,不用考虑同步问题
done_code = new Long(doneCode);
s_doneCode.set(done_code);
}
return done_code.longValue();
}
public static void setDoneCode(long doneCode) {
s_doneCode.set(new Long(doneCode));
}
public static void clearDoneCode() {
s_doneCode.remove();
ServiceManager.setDoneCode(null);
}
JDK源码:
ThreadLocal类:
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
/**
* Get the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
*
* @param t the current thread
* @return the map
*/
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}