I am using InheritableThreadLocal in the Servlet class. So child flows will be accessible from it. Is this evil using InheritableThreadLocal in thread pool executors ?, such as servlet thread pool.
My questions.
1) Why should we avoid using InheritableThreadLocals in servlets?
2) Is memory leak possible in InheritableThreadLocal?
3) Is there an alternative for InheritableThreadLocal ?.
4) What happens if the thread is reused, the value stored in threadlocal will not be cleared?
My real time script
public class UserAccessFilter implements javax.servlet.Filter { static final InheritableThreadLocal<String> currentRequestURI = new InheritableThreadLocal<String>(); public void doFilter(ServletRequest req, ServletResponse resp , FilterChain fc) throws IOException, ServletException{ String uri = request.getRequestURI(); fc.doFilter(request, response); } } public class MailServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String mailCount = req.getParameter("mailCount"); if(mailCount != null && !"".equals(mailCount) && mailCount.matches("[0-9]+")){ MailThread mailThread = new MailThread(" xxx@gmail.com ", generateToAddress(Integer.parseInt(mailCount)));
Filter β Servlet A β Child Thread ---> Mail Thread (Here I get the value specified in the filter).
source share