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示例

  • 2013年03月24日 17:40
  • 6KB
  • 下载

java事务 - threadlocal

  • 2014年09月13日 12:46
  • 1.07MB
  • 下载

java的ThreadLocal类的用法

java的ThreadLocal类的用法,ThreadLocal是一个支持泛型的类,用在多线程中用于防止并发冲突问题。 例如下面的一个例子,就是用于线程增加1,但是相互不冲突 package co...

JAVA ThreadLocal类深入

  • 2011年07月31日 10:20
  • 47KB
  • 下载

Java多线程之ThreadLocal和InheritableThreadLocal的使用

类ThreadLocal的使用 类变量的攻下那个可以使用public static变量的形式,所有的线程都使用同一个public static变量,如果想实现每一个线程都有自己的共享变量该如何解决...

Java多线程探究-线程局部变量ThreadLocal

TheadLocal可以为各个线程提供各自的实例。例如SimpleDateFormat是非线程安全的 假设有一个静态变量 public static final SimpleDateFormat ...

java多线程学习(四)---ThreadLocal和单例实现线程内对象的共享

public class ThreadLocalTest { private static ThreadLocal x = new ThreadLocal(); private sta...

Java并发编程:深入剖析ThreadLocal

想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理。首先,本文先谈一下对ThreadLocal的理解,然后根据ThreadLocal类的源码...

Java学习整理系列之ThreadLocal的理解

在同步机制中,通过对象的锁机制保证同一时间只有一个线程访问变量。这时该变量是多个线程共享的,使用同步机制要求程序慎密地分析什么时候对变量进行读写,什么时候需要锁定某个对象,什么时候释放对象锁等繁杂的问...

【JAVA秒会技术之多线程】彻底理解ThreadLocal

ThreadLocal是什么   早在JDK 1.2的版本中就提供Java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路。使用这个工具类...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java ThreadLocal proplem
举报原因:
原因补充:

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