...
Wiki Markup |
---|
The use of {{ThreadLocal}} objects requires care in classes whose objects are required to be executed by multiple threads in a thread pool. The technique of thread pooling allows threads to be reused when thread creation overhead is too expensive or when creating an unbounded number of threads can diminish the reliability of the system. Every thread that enters the pool expects to see an object in its initial, default state. However, when {{ThreadLocal}} objects are modified from a thread whichthat is subsequently made available for reuse, the reused thread sees the state of the {{ThreadLocal}} object as set by the previous thread \[[JPL 2006|AA. Java References#JPL 06]\]. |
...
In this execution order, it is expected that the two tasks (t2 and t3) started from doSomething2()
are expected to would observe the current day as Monday. However, because pool thread 1 is reused, t3 observes the day to be Friday.
...
TPS04-EX1: There is no need to reinintialize reinitialize a ThreadLocal
object that does not change state after initialization. For example, there may be only one type of database connection represented by the initial value of the ThreadLocal
object.
...