为什么我应该避免在 servlet 中使用 InheritableThreadLocal? [英] Why should I avoid using InheritableThreadLocal in servlets?

查看:32
本文介绍了为什么我应该避免在 servlet 中使用 InheritableThreadLocal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Servlet 类中使用了 InheritableThreadLocal.这样它就可以从它的子线程中获得.在线程池执行程序中使用 InheritableThreadLocal 是邪恶的吗?.比如servlet线程池.

I am using InheritableThreadLocal in my Servlet Class. So that It will available from it's child threads. Is that evil using InheritableThreadLocal in thread pool executors? . Such as servlet thread pool.

我的问题.

1) 为什么我们应该避免在 servlet 中使用 InheritableThreadLocals?

1) Why should we avoid using InheritableThreadLocals in servlets?

2) 在 InheritableThreadLocal 中是否可能出现这种内存泄漏?

2) Is this memory leak possible in InheritableThreadLocal?

3) InheritableThreadLocal 有什么替代方案吗?.

3) Is there any alternative for InheritableThreadLocal?.

4) 如果线程被重用,存储在threadlocal中的值不会被清除会发生什么?

4) What happens if the thread will be reused , the value stored in threadlocal will not be cleared?

我的实时场景

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))); //NO I18N
        Thread t = new Thread(mailThread);
        t.start();
    }

    resp.getWriter().println("Mail Servlet.............."); //NO I18N

}              

}

class MailThread implements Runnable{

private String from;
private String to;

public MailThread(String from , String to){
    this.from = from;
    this.to = to;
}


@Override
public void run() {
    sendMail();
}

    public void sendMail(){
        //I want this uri from child threads. I can't pass this value to this constructor.
        String uri = currentRequestURI.get();
        //Do Mail Operations
    }


}

过滤器 --> Servlet A --> 子线程 ---> 邮件线程(这里我得到了过滤器中设置的值).

Filter --> Servlet A --> Child Thread ---> Mail Thread (Here I am getting the value setted in filter) .

推荐答案

为什么我们应该避免在 servlet 中使用 InheritableThreadLocals?

Why should we avoid using InheritableThreadLocals in servlets?

它们代表了将信息从一个请求泄露到另一个请求的潜在路径.问题"是请求由线程池处理.当 on request 完成时,线程处理的下一个请求很可能是针对不同的用户的.但是,如果您在完成第一个请求时忘记清除线程本地状态,则它可能会被第二个请求使用.

They represent a potential path for leaking information from one request to another. The "problem" is that requests are handled by a pool of threads. When on request is completed, the next request that a thread handles is likely to be for a different user. But if you forget to clear the thread local state on finishing the first request, there is a potential that it might be used by the second one.

InheritableThreadLocal 是否可能存在内存泄漏?

Is a memory leak possible in InheritableThreadLocal?

是的......有点.如果我们假设工作池是有界的,那么任何线程的线程本地状态都有可能被覆盖,从而清除内存泄漏.最糟糕的问题是有界内存泄漏……受池中线程数的限制.

Yes ... sort of. If we assume that the worker pool is bounded, the thread local state of any thread is likely to be overwritten, clearing the memory leak. At worst the problem is a bounded memory leak ... bounded by the number of threads in the pool.

信息泄露问题更令人担忧.

The information leakage problem is more concerning.

InheritableThreadLocal 有什么替代方案吗?

Is there any alternative for InheritableThreadLocal?

在请求或响应对象中设置属性更好.

Setting attributes in the request or response object is better.

如果线程会被重用会发生什么,存储在threadlocal中的值不会被清除.

What happens if the thread will be reused, the value stored in threadlocal will not be cleared.

它不会被清除.这就是问题所在!

It won't be cleared. THAT is the problem!

这篇关于为什么我应该避免在 servlet 中使用 InheritableThreadLocal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆