首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JDK的ThreadLocal了解(三)优雅的使用ThreadLoca

2013-08-13 
JDK的ThreadLocal理解(三)优雅的使用ThreadLocapublic class ActionContext implements Serializable {sta

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处使用都很简单,不容易出错

?

热点排行