My point stands, there is no API provided. The thread local cleaner that is
used, while used commonly in many applications (I am 99% sure I saw that
exact code or very similar in a SO while looking for such an API), is still
dependent on internal implementation details of thread locals
(non-public/inaccessible fields). This is no different than depending on
Unsafe. As you mentioned, it is much harder in later versions of Java
because implementation details are much more encapsulated.

-Paul


On Mon, Oct 18, 2021 at 4:24 AM Robert Munteanu <[email protected]> wrote:

> Hi,
>
> On Sun, 2021-10-17 at 14:26 -0500, Paul Bjorkstrand wrote:
> > As far as I understand, thread locals' storage is an implementation
> > detail
> > in the JVM. There isn't an API to clear all/arbitrary thread locals.
> > Thread
> > pools, or applications that use thread pools, need to provide hooks to
> > do
> > that, so that code can do its own cleanup. This is exactly what the
> > request listener that Carsten mentioned does.
>
> Sling's thread pool actually cleans up thread locals after execution
> [1]. It's not yet fixed for Java 17 though [2].
>
> [1]:
>
> https://github.com/apache/sling-org-apache-sling-commons-threads/blob/org.apache.sling.commons.threads-3.2.22/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolExecutorCleaningThreadLocals.java
> [2]: https://issues.apache.org/jira/browse/SLING-10831
>
> Thanks,
> Robert
>

Reply via email to