java ThreadLocal proplem

原创 2016年12月22日 11:11:07
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跳点

Java学习记录--ThreadLocal使用案例

java学习记录–ThreadLocal使用案例标签(空格分隔): java最近整理公司项目,发现不少写的比较糟糕的地方,比如下面这个:public class DateUtil { priva...
  • u012706811
  • u012706811
  • 2016年11月19日 16:26
  • 1805

【Java并发编程】深入分析ThreadLocal(八)

我们在使用一个类时,首先要知道它能做什么,然后再去深入分析它的工作原理。ThreadLocal如果从名字上来看像是“本地线程"的意思,其实ThreadLocal并不是一个线程,而是线程的局部变量。当使...
  • liulongling
  • liulongling
  • 2016年01月29日 15:56
  • 3815

深入学习理解java-ThreadLocal

ThreadLocal是什么?有什么用?怎么用? 1。ThreadLocal是什么 带着这样的问题我们来学习一下java并发编程中的一个重要的角色分子ThreadLocal。 早在JDK 1.2...
  • baidu_23086307
  • baidu_23086307
  • 2017年02月23日 16:05
  • 2195

Java ThreadLocal解决线程安全问题

TLS:Thread Local Storage   转载自http://blog.csdn.net/jiht594/article/details/6606326  TLS全称为Thread L...
  • bestcxx
  • bestcxx
  • 2017年02月10日 23:11
  • 592

ThreadLocal父子线程传递实现方案

前言 介绍InheritableThreadLocal之前,假设对 ThreadLocal 已经有了一定的理解,比如基本概念,原理,如果没有,可以参考:ThreadLocal源码分析解密.在讲解...
  • a837199685
  • a837199685
  • 2016年09月30日 17:06
  • 5768

java 深入理解ThreadLocal

相信读者在网上也看了很多关于ThreadLocal的资料,很多博客都这样说:ThreadLocal为解决多线程程序的并发问题提供了一种新的思路;ThreadLocal的目的是为了解决多线程访问资源时的...
  • mccand1234
  • mccand1234
  • 2017年01月07日 13:49
  • 912

JAVA ThreadLocal对象浅析

最近在开发过程中,在做一个字典项服务的时候,最开始采用了ThreadLocal对象来缓存数据。在使用ThreadLocal过程中遇到一些问题,这里和大家分享一下。 一、 什么是ThreadLocal...
  • a564663276
  • a564663276
  • 2015年03月11日 09:52
  • 649

java多线程并发控制之ThreadLocal

下面是ThreadLocal的测试代码,更多信息请参考注释 package com.jadyer.thread.local;      import java.util.Rand...
  • maguanghui_2012
  • maguanghui_2012
  • 2017年02月11日 11:41
  • 732

Java-ThreadLocal的简单例子

内容:ThreadLocal:允许我们创建只能被同一个线程读写的变量,例如Web应用中将变量从前端到后台,并且需要在这次请求的线程中始终可以随时获取到。内部实现是通过一个ThreadLocalMap这...
  • u011345136
  • u011345136
  • 2015年06月02日 22:53
  • 1434

java多线程--深入理解threadlocal以及适用场景

如何使用:       简介: JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路。使用这个工具类可以很简洁地编...
  • sean417
  • sean417
  • 2017年04月10日 15:51
  • 771
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java ThreadLocal proplem
举报原因:
原因补充:

(最多只允许输入30个字)