Index Page : Link

Donate : Link

WordPress Blog : Link

  • ThreadLocal class provides thread local variable.
  • ThreadLocal class maintains values per thread basis.
  • Each ThreadLocal object maintains a seperate value like user id, transaction id, etc… for each thread that accesses that object.
  • Thread can access it’s local value, can manipulate it’s value and even can remove it’s value.
  • In every part of the code which is executed by the thread we can access it’s local variable.
  • Ex. Consider a servlet which invokes some business methods, we have a requirement to generate a unique transaction id for each and every request and we have to pass this transaction id to the business methods for this requirement we can use ThreadLocal to maintain a seperate transaction id for every request that is for every thread.

Note :

  1. ThreadLocal class introduced in 1.2v and enhanced in 1.5v.
  2. ThreadLocal can be associated with thread scope.
  3. Total code which is executed by the thread has access to the corresponding thread local variables.
  4. A thread can access it’s own local variables and can’t access other thread local variables.
  5. Once a thread entered into dead state all it’s local variables by default avaialble/eligible for garbage collection (GC) .

Constructor :

1. ThreadLocal tl = new ThreadLocal();

  • Creates a thread local variable.

Methods :

1. public T get()

  • Returns the value of ThreadLocal variable associated with current thread.

2. protected Object initialValue()

  • Returns initial value of ThreadLocal variable associated with current thread.
  • The default implementation of this method return null.
  • To customize our own initialize value we have tooverride this method.

3. public void set(Object value)

  • To set a new value

4. public void remove()

  • To remove the value of ThreadLocal variable associated with current thread.
  • It is newly added method in 1.5v.
  • After removal if we are trying to access it will be reinitialized once again by invoking it’s initialValue() method.

Example 1 :

Example 2 :

Example 3 :

Output :

  • In the above program for every customer thread a seperate customer id will be maintained by ThreadLocal object.
  • If you didn’t get proper output then uncomment line #1 and #2.
  • Parent thread’s ThreadLocal variable by default not available to the child thread.
  • If we want to make parent thread’s ThreadLocal variable value available to the child thread then we should go for InheritableThreadLocal class.
  • By default child thread’s value is exactly same as parent thread’s value but we can provide customized value for child thread by overriding childValue() method.

Constructors :

1. InheritableThreadLocal t = new InheritableThreadLocal();

Methods :

  • InheritableThreadLocal is a child class of ThreadLocal and hence all method’s present in ThreadLocal by default available to InheritableThreadLocal.
  • In addition to InheritableThreadLocal it contains only 1 method Object childValue(Object parentValue).

Output :

  • In the above program if replace InheritableThreadLocal with the ThreadLocal and if we are not overriding childValue() method then the output is
  • In the above program if we are maintaining InheritableThreadLocal and if we are not overriding childValue() then the output is