Thursday, October 8, 2015


The InheritableThreadLocal class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values.

I used to think ThreadLocal is one instance per thread. Actually it is wrong. You can think ThreadLocal as a map. The key is the thread id, the value is the value for that thread. ThreadLocal is one instance for all threads.

In a typical 3-tier application, presentation - service - persistence. You may want to set a ThreadLocal value in presentation layer and read it in persistence layer. To get the reference of ThreadLocal, we need make the ThreadLocal static.

No comments:

Post a Comment