Ad

Thursday, May 23, 2013

ThreadLocal in thread pools

ThreadLocals not happy in Thread Pools


I recently ran into a problem where i was trying to propogate a value over using a ThreadLocal.
The problem was i was doing it in a scenario where the threads being executed where in a thread pool which cause problems where ThreadLocal values where being shared between different threads.

A better approach is to have a static map which allows you to use the current thread (Thread.currentThread()) to map to your value. Just remember to clear the entry from the map when your done using it so the thread can die.

http://blog.maxant.co.uk/pebble/2008/09/23/1222200780000.html

No comments: