JDK的ThreadLocal理解(三)优雅的使用ThreadLoca
public class ActionContext implements Serializable { static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>(); /** * Sets the action context for the current thread. * * @param context the action context. */ public static void setContext(ActionContext context) { actionContext.set(context); } /** * Returns the ActionContext specific to the current thread. * * @return the ActionContext for the current thread, is never <tt>null</tt>. */ public static ActionContext getContext() { return actionContext.get(); }}
?经过这种封装后,整个ActionContext类,只有3个地方会涉及ThreadLocal的使用:变量定义、set、get。这3处使用都很简单,不容易出错
?