ThreadLocal e ThreadPool non interagiscono tra loro a meno che non lo facciate.
Quello che si può fare è un singolo ThreadLocal che memorizza tutto lo stato che si desidera mantenere e deve essere ripristinato al termine dell'attività. È possibile ignorare ThreadPoolExecutor.afterExecute (o BeforeExecute) per cancellare il tuo ThreadLocal (s)
Da 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) { }
Invece di tenere traccia di tutte le ThreadLocals, li si potrebbe cancellare in una sola volta.
protected void afterExecute(Runnable r, Throwable t) {
// you need to set this field via reflection.
Thread.currentThread().threadLocals = null;
}
fonte
2015-05-19 14:36:33
Non ti risponde alla tua domanda? Dici: "se viene usato lo stesso thread, allora sto trovando dati obsoleti". Allora, qual è la tua domanda allora? – GhostCat
Che pool di thread? – erickson