Die ThreadLocal und ThreadPool interagieren nicht miteinander, wenn Sie dies tun.
Was Sie tun können, ist ein einzelner ThreadLocal, der den gesamten Zustand speichert, den Sie behalten möchten, und dieser muss zurückgesetzt werden, wenn die Aufgabe abgeschlossen ist. Sie können außer Kraft setzen ThreadPoolExecutor.afterExecute (oder BeforeExecute) Ihre Thread zu löschen (s)
Von 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) { }
Anstatt zu verfolgen alle ThreadLocals, können Sie sie alle auf einmal löschen können.
protected void afterExecute(Runnable r, Throwable t) {
// you need to set this field via reflection.
Thread.currentThread().threadLocals = null;
}
Sind Sie nicht Ihre eigene Frage zu beantworten? Sie sagen: "Wenn der gleiche Thread verwendet wird, finde ich veraltete Daten". Also, was ist deine Frage? – GhostCat
Welcher Thread-Pool? – erickson