What is ThreadLocal in java : ThreadLocal in java is a class which enables you to create thread-safe local variables. ThreadLocal is another way to achieve thread-safety .
Why ThreadLocal variables are thread safe?
ThreadLocal variables are thread-safe as every thread has its own set of ThreadLocal variables which can only be read and write by the same thread and other threads cannot see these ThreadLocal variables.Thread can holds ThreadLocal variables till it is not in dead state.
When ThreadLocalVariable is eligible to garbage collection?
ThreadLocal variable becomes eligible to Garbage collection after thread died or due to any Exception with the condition that ThreadLocal variable doesn’t have any other live references.
Program for using ThreadLocal :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Date; class ThreadLocalRunnable implements Runnable { private ThreadLocal threadLocal = new ThreadLocal(); @Override public void run() { threadLocal.set(new Date().toString()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" processing time is : "+threadLocal.get()); } } public class ThreadLocalDemo { public static void main(String[] args) throws InterruptedException { ThreadLocalRunnable threadLocal = new ThreadLocalRunnable(); Thread threadA = new Thread(threadLocal,"Thread-A"); Thread threadB = new Thread(threadLocal,"Thread-B"); Thread threadC = new Thread(threadLocal,"Thread-C"); threadA.start(); threadA.join(); threadB.start(); threadB.join(); threadC.start(); } } |
OutPut:
Thread-A processing time is : Thu May 04 17:36:36 IST 2017
Thread-B processing time is : Thu May 04 17:36:37 IST 2017
Thread-C processing time is : Thu May 04 17:36:38 IST 2017
Where to use ThreadLocal:
1)In multithreaded environment, each thread will start at different time and we might need to store time at which they have started. So, we can store thread’s start time in ThreadLocal.
2)Many web frameworks use ThreadLocal for maintaining context may be session or request related value.For example : In a multithreaded web application we have a requirement to generate a unique transaction id for each and every user request .We need to pass this transaction id to all the business methods. One way is to provide transaction id as a parameter to all the business methods. But this is not a good solution as the code is redundant and unnecessary.So, can use ThreadLocal variable here by setting this transaction id in the ThreadLocal. Now, all the business methods in this controller can access this transaction id from the ThreadLocal . Also,multiple user requests are coming and each request is processed y separate thread, so transaction id will be unique to each thread .
3)You can make any non Thread Safe object as thread- safe by wrapping in ThreadLocal
4)J2EE application servers like Weblogic,Websphere uses java ThreadLocal to keep track of transaction and security Context.