I have a third party Java library that provides a resource.
TheResource resource = TheResource.create()
Sometime later, when I'm done with the resource, I'm supposed to close the resource
resource.close()
According to the library documentation, the resource I create is a ThreadLocal variable. That means the resource is created per thread, and it sticks to the thread. My question is, is it possible to call the close()
method automatically when the associated thread is finished? I think I can do it if the thread is my own custom thread because I can engage with the lifecycle of the thread. But my question is about those threads provided by the system.
For instance, in Android's SyncAdapter provides a thread for you. And I don't know how the thread is finished. I'm wondering if there's some kind of 'hook' mechanism in terms of threads' lifecycle so that I can close ThreadLocal resources of the threads.