I had an awful time with ThreadLocal today. Apparently the get() creates a new ThreadLocalMap for the thread (when one does not exist) after invoking the initialValue().

Current Implementation:


*public* Object get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
*if* (map != null) *return* map.get(*this*);


       Object value = initialValue();
       createMap(t, value);
       *return* value;
   }

So if the initialValue() created another thread local variable, that variable is completely lost by the time get() is complete. It is not a very big deal had it been documented properly. I spent several hours trying to figure this out. Although a better implementation would be the following, I think.

Would be nice implementation:


*public* Object get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
*if* (map != null) *return* map.get(*this*);


Object value = initialValue();

*if* (map != null) map.set(*this*, value);
*else*
createMap(t, value);


       *return* value;
   }

So I finally fixed the ThreadLocalStorage problem, I guess! Howard, can I have karma to the sandbox cvs?

-Harish


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to