I had seen the following code in a J2EE project.
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();
}
}
It seems that by having ThreadLocal
and a static get
, we will have an easy way to obtain the current RequestContext of current thread.
But, is this a common practice? Is it a correct way to do so? Is there a chance of having memory leak?
Why the object created by ClassLoader do not have chance to garbage collect itself