Showing posts with label ThreadLocal. Show all posts
Showing posts with label ThreadLocal. Show all posts

Tuesday, August 4, 2015

ThreadLocal and Inheritable ThreadLocal


I am not going to share any new concept or library of java. My topic of this post related to ThreadLocal that is with us since JDK version 1.2 but after discussion with many developers with in my circle I came to know that many developer doesn't know it feature and functionality.

Thread Local storage is just like class that can store/return object with in thread scope. Just like application context, session or request scope with in servlet.

Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread [wikipedia].

ThreadLocal instances are typically private static fields in classes that usually maintain thread state 

This class is very useful if you want to share common objects into different applications layers like users credentials, connection objects etc. I usually use this class after wrapping it in my ThreadContext class. Following are the code of my ThreadContext class. 

To save my common object with in thread I usually call it's static put method and when I need to use that saved object I call it's static object.