Is it really my job to clean up ThreadLocal resources when classes have been exposed to a thread pool?

前端 未结 5 1228
臣服心动
臣服心动 2021-01-31 14:21

My use of ThreadLocal

In my Java classes, I sometimes make use of a ThreadLocal mainly as a means of avoiding unnecessary object creation:



        
5条回答
  •  一生所求
    2021-01-31 15:08

    If its any help I use a custom SPI (an interface) and the JDK ServiceLoader. Then all of my various internal libraries (jars) that need to do unloading of threadlocals just follow the ServiceLoader pattern. So if a jar needs threadlocal cleanup it will automatically get picked if it has the appropriate /META-INF/services/interface.name.

    Then I do the unloading in a filter or listener (I had some issues with listeners but I can't recall what).

    It would be ideal if the the JDK/JEE came with a standard SPI for clearing threadlocals.

提交回复
热议问题