Quadir
Quadir

Reputation: 271

In Java, when ThreadLocal objects are modified, will the changes persist in the next request?

In a typical web application, when a request comes in, a filter looks for a Context object in http session. If one doesn't exist, it creates the Context object and stores it in http session. Additionally, this Context object is also stored in the ThreadLocal object. A Servlet down the path retrieves this Context object from ThreadLocal and modifies it. When the response is returned, the filter now nulls the Context object in ThreadLocal. So when the user makes another request, will he able to see the modified Context object?

Thanks Quadir

Upvotes: 3

Views: 3475

Answers (4)

Adriaan Koster
Adriaan Koster

Reputation: 16209

yes, the user will see the Context object because a reference to it was stored in the HttpSession. Even though the reference in the ThreadLocal was nulled, it will still be found in the session during the second request.

EDIT: In the OpenJDK sourcecode of ThreadLocal (from line 410) you can see the difference between ThreadLocal's set and remove methods. Calling set(null) will leave the ThreadLocalMap entry in place with a null value whereas remove() will delete it entirely. It won't make a difference with regard to your question, there will still be a reference to your Context object in the Session.

When I first read your question title I interpreted it differently because there was no mention of the HttpSession or clearing the ThreadLocal. Maybe this confused some of the responders. It sounded like you wanted to know if a ThreadLocal variable set in the first request (and not cleared) would still be accessible in the second request. I think the answer is that this depends on how your webserver handles threads. If there is a threadpool of 10 threads that are randomly reused you should have a 10% chance of finding the same ThreadLocal variable in the second request.

Upvotes: 3

Prabath Siriwardena
Prabath Siriwardena

Reputation: 5921

No - it won't be visible to next request unless you are using a Thread Pool. But instead of making it null - better you call remove() on thread local instance - otherwise you may lead to memory leaks..

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346407

That depends on the context. Session and application contexts are explicitly restored, while request contexts and other ThreadLocals can absolutley not be relied upon to persist between requests, since the same thread could (and usually will) handle a request from a completely different user. I believe some (if not all) web containers deliberately delete all ThreadLocals to avoid having users share data accidentally.

Upvotes: 0

Bozho
Bozho

Reputation: 597254

No. If you null it (or better call threadLocal.remove()) the value will be lost.

If you don't it might be visible to some of the next request to which the same thread is assigned (servlet containers use a thread pool). But this is an undesirable side-effect of thread-pools - you should always clean your threadlocals. (see here)

Upvotes: 3

Related Questions