views:

184

answers:

4

Is there a way to list TheadLocals bound to a thread? Ideally I could access the Thread.threadLocals map, but it is package protected.

The reason I need this is I need to inspect threads as they are returned to a thread pool to ensure the ThreadLocals have been properly cleaned up. Perhaps there is another way to do this?

A: 

You could use the afterExecute method of the thread pool to do whatever cleanup (reinitializiation?), as long as you know which variables you want to clean up.

Otherwise you could use reflection - from within the thread, iterate through the declared fields of the class(es) youre interested in, and for each one whose type is an instance of ThreadLocal, set it to its initialValue on the object(s) you care about.

danben
+1  A: 

The reason I need this is I need to inspect threads as they are returned to a thread pool to ensure the ThreadLocals have been properly cleaned up.

Personally, I think using thread local data on threadpool threads is a bad practice. If you really need thread local state, you should be self managing the threads, so you can explicitly cleanup the data.

Thread pool threads have indeterminate lifetimes, so you shouldn't be relying on explicitly managed local thread data.

Reed Copsey
Agreed. But I want to fix a bug in existing code, not re-architect it.
landon9720
A: 

You could use AOP-like construct, by creating a Runnable implementation that wraps the original Runnable by your own implementation. It will invoke the original Runnable's run method and then perform any other cleanup you need from within the thread's context, which will allow you to call the ThreadLocal.remove() method. Then, give this wrapper to the thread pool. This would work for any thread pool implementation (e.g. ones without before/afterExecute methods)

Armadillo
Hooking into the thread lifecycle isn't the problem. I am using ThreadPoolExecutor, so I have the afterExecute method to work with. The problem is given a thread, how do I enumerate thread local storage? I'd like to check that it is empty, and if not log a warning.
landon9720
Do you have control over the thread local usage (can you tell the developers to use your own implementation)? You may be able to extend ThreadLocal to help with achieving your goal.
Armadillo
A: 

From the sources, it looks to be pretty tight. Everything is private to either Thread or ThreadLocal.

You might be able to do what you need through an instrumentation agent by redefining ThreadLocal to add a method that will dump the locals on the current thread.

Here's an example I found for adding logging to an existing class: http://today.java.net/pub/a/today/2008/04/24/add-logging-at-class-load-time-with-instrumentation.html

You'll need to use BCEL or JavaAssist to patch ThreadLocal bytecode to add the method. Later on, you'll need to use reflection to get a handle to the method so you can call it.

Note: this probably won't work if you are running in a restricted environment (applet or appserver) as the security mechanisms generally prevent you from mucking about with system classes.

Devon_C_Miller