java ThreadLocal proplem

private HttpServletRequest request;
private static ThreadLocal<WebContext> wcHolder = new ThreadLocal<WebContext>();
    private WebContext(){}

    public static WebContext newInstance(HttpServletRequest request){
//case 1:correct
//        WebContext wc = new WebContext();
//        wc.request = request;

//case 2:incorrect
//(to resolve multiply creat WebContext object within one request)
        WebContext wc = wcHolder.get();
        if(wc==null){
            wc = new WebContext();
            wc.request = request;
            wcHolder.set(wc);
        }
        return wc;
    }

case 1 is correct.

but case 2

first tomcat request..
second request may get the WebContext object which belong to first req.
when step 2 happened,WebContext’s request object had diabled.
then problem happened when operate on the wc and the request object!
the question is:

is one ThreadLocal bind on Thread not right?
is one tomcat request is one Thread not right?
how correct above code ?
ps:sorry,my english is bad. :)

comment

Seeing code that captures a reference to an HttpServletRequest leads me to believe that, very soon, you will be posting a question about why your application is behaving strangely, usually confusing a response for one request as another. Be very careful when capturing a reference to a request object somewhere like a ThreadLocal. – Christopher Schultz 3 hours ago

yes,you are right.the problem had happened ,when refresh page repeatly,because get previous request obj by threadLocal.get(). i guess tomcat just assign the same thread for the repeatly browser request that moment.then app throw nullpoint error,i invoke request.getSession().getAtt.. – wangshao 2 hours ago

thanks@John Bollinger,@Christopher Schultz.now the problem had resolved. invoke WebContext.reset(),then method do ThreadLocal.remove(), within spring interceptor.preHandle method.ps thanks more patient for my bad english.i am try better.:) – wangshao 2 hours ago

ANSWER

is one ThreadLocal bind on Thread not right?
A ThreadLocal binds a separate object to each thread, yes. That’s its purpose.

is one tomcat request is one Thread not right?
Each request is handled by exactly one thread, but I’m fairly certain that Tomcat does not spawn a new thread for each request. It uses a thread pool.

Also, different requests in the same session may be served by different threads.

how correct above code ?
If the previous answers don’t explain it then I don’t know, because it’s not clear in what way you consider the behavior you observe to be incorrect.

Nevertheless, if your intent is for each request to have exactly one WebContext, unique to it, then ThreadLocals seem a poor choice for managing that. Request attributes, on the other hand, are designed for exactly that purpose. See ServletRequest.getAttribute(), ServletRequest.setAttribute(), etc..

参考资料

stack overflow跳点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值