2
我在J2EE項目中看到了以下代碼。以下技術是否正確獲取RequestContext
public class RequestContext {
private final static ThreadLocal<RequestContext> contexts = new ThreadLocal<RequestContext>();
/* Initialization */
public static RequestContext begin(ServletContext ctx, HttpServletRequest req, HttpServletResponse res) {
RequestContext rc = new RequestContext();
..
contexts.set(rc);
return rc;
}
public static RequestContext get(){
return contexts.get();
}
}
似乎具有ThreadLocal
和靜態get
,我們將有一個簡單的方法來獲得當前線程的當前的RequestContext。
但是,這是一種常見的做法嗎?這是否是正確的方法?有沒有內存泄漏的機會?
Why the object created by ClassLoader do not have chance to garbage collect itself