Reputation: 38885
Today when I debug my app into RequestContext
putLoginId method, I found the class static ThreadLocal initial failed, the contextData.get()
return null, the static contextData only have threadLocalHashCode.
My contextData
define in RequestContext
class like this:
public class RequestContext {
public static final ThreadLocal<Map<String, Object>> contextData = new ThreadLocal<>();
}
the contextData
should be initialed when load the class, so I think it will never be null. But why this happening and what should I do to fix it? Am I missing something?
Upvotes: 2
Views: 277
Reputation: 140484
the contextData should be initialed when load the class, so I think it will never be null
contextData
is initialized when the class is loaded; but you don't actually give it a (per-thread) value, at least in the code shown.
Give your ThreadLocal
an initial value with the factory method, for example:
public static final ThreadLocal<Map<String, Object>> contextData =
ThreadLocal.withInitial(HashMap::new);
Upvotes: 4