I'm using ThreadLocal
in a servlet under the assumption that any reference to the ThreadLocal value won't outlive the request. This assumption is based on the fact that servlets kick off a thread for each request. One thing I'm unsure of is what happens when the thread goes into a thread pool -- If this happens, can a ThreadLocal
value set during a previous request be present in a subsequent request if the same thread is used to service it? Or is it considered a "new" thread as far as java is concerned?
Asked
Active
Viewed 22 times
0

w.brian
- 16,296
- 14
- 69
- 118
-
Please check this question: http://stackoverflow.com/questions/7403809/java-cached-thread-pool-and-thread-local – dotvav Jul 30 '15 at 14:27
-
If you are using a Thread Pool it is not a new thread each time and the ThreadLocal will be retained. You can clear all ThreadLocals for a thread with a hack, but it is better if you can change your code so you don't need to do this. – Peter Lawrey Jul 30 '15 at 14:32