Will the ThreadLocal object be cleared after thread returned to Thread Pool?
Asked Answered
A

4

20

Will the contents that are stored in the ThreadLocal storage during an execution be cleared automatically when the thread is returned to ThreadPool (as would be expected) ??

In my application I am putting some data in ThreadLocal during some execution but if next time the same Thread is being used, then I am finding the obsolete data in ThreadLocal storage.

Aframe answered 19/5, 2015 at 14:24 Comment(1)
Aren't you answering your own question? You say: "if the same thread is being used, then I am finding obsolete data". So, what is your question then?Mablemabry
R
18

The ThreadLocal and ThreadPool don't interact with one another unless you do this.

What you can do is a a single ThreadLocal which stores all the state you want to hold and have this be reset when the task completes. You can override ThreadPoolExecutor.afterExecute (or beforeExecute) to clear your ThreadLocal(s)

From ThreadPoolExecutor

/**
 * Method invoked upon completion of execution of the given Runnable.
 * This method is invoked by the thread that executed the task. If
 * non-null, the Throwable is the uncaught {@code RuntimeException}
 * or {@code Error} that caused execution to terminate abruptly.
 *
 * <p>This implementation does nothing, but may be customized in
 * subclasses. Note: To properly nest multiple overridings, subclasses
 * should generally invoke {@code super.afterExecute} at the
 * beginning of this method.
 *
... some deleted ...
 *
 * @param r the runnable that has completed
 * @param t the exception that caused termination, or null if
 * execution completed normally
 */
protected void afterExecute(Runnable r, Throwable t) { }

Rather than keep track of all ThreadLocals, you could clear them all at once.

protected void afterExecute(Runnable r, Throwable t) { 
    // you need to set this field via reflection.
    Thread.currentThread().threadLocals = null;
}
Resinoid answered 19/5, 2015 at 14:36 Comment(3)
dude... and what if ThreadPoolExecutor uses some thread local variables.Sauropod
In enterprise environments setting (package) protected fields via reflection is an illegal security breach! :(Plagiarism
@Plagiarism just because you can do something, doesn't mean you should.Resinoid
S
17

No. As a principle, whoever put something in thread local should be responsible to clear it

threadLocal.set(...);
try {
  ...
} finally {
  threadLocal.remove();
}
Sauropod answered 19/5, 2015 at 17:9 Comment(1)
This saved my day!! important thing I was missing in my code.Piglet
S
3

Will the contents that are stored in the ThreadLocal storage during an execution be cleared automatically when the thread is returned to ThreadPool

No. ThreadLocals are associate with Threads, not with execution of a Callable/Runnable passed to a threadpool's task queue. Unless explicitly cleared - @PeterLawrey gives an example on how to do so - ThreadLocals and their state persist through multiple task executions.

Sounds like you can achieve the desired behavior with a local variable declared in your Callable/Runnable

Seeley answered 24/4, 2018 at 23:8 Comment(0)
D
2

No, the ThreadLocal object is associated to a thread and not a task. Hence ThreadLocal should be used carefully with Thread Pool. As a principle thread local makes sense with thread pool only when thread local life cycle aligns with life cycle of task

Dulcimer answered 15/1, 2020 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.