The ThreadLocal class in Java provides a local variable for Threads. ThreadLocal class maintains values per Thread basis. Each Thread Local object maintains separate values like transaction id user id etc. For each Thread object Thread can access its local value also can manipulate its value and even can remove its value. In every part of the code which is executed by the thread, we can access its local variable.
For Example- Consider a Servlet which invokes some business methods. We have to generate a unique transaction id for every request and pass this transaction id to the business method. For this, we can use ThreadLocal to maintain a separate transaction id for every request i.e., for every thread.
Note-
- ThreadLocal class was introduced in Java 1.2 v and enhanced in Java 1.5v.
- ThreadLocal can associate with Thread scope.
- All code executed by Thread has access to the corresponding ThreadLocal variable.
- A Thread can access its local variable and can not access other Threads’ local variables.
- Once a Thread is entered into a ready state then all its local variables are by default available for garbage collection.
ThreadLocal Constructor
ThreadLocal varibale = new ThreadLocal();
It’s create a ThreadLocal variable
ThreadLocal Methods
- T get()
This method returns the value of the thread- local variable associated with the current thread.
- void set( T value )
This method is used to set a new value.
- void remove()
This method is used to remove the value of the thread – local variable associated with the current thread. This method was introduced in Java 1.5v.
- initialValue()
This method returns the initial value of the thread – the local variable associated with the current thread.
public class ThreadLocalClass {
public static void main(String[] args) {
ThreadLocal threadLocal = new ThreadLocal();
System.out.println(threadLocal.get());
threadLocal.set("The Code Data");
System.out.println(threadLocal.get());
threadLocal.remove();
System.out.println(threadLocal.get());
}
}
initialValue() Overrriding
public class ThreadLocalClass {
public static void main(String[] args) {
ThreadLocal threadLocal = new ThreadLocal() {
@Override
protected Object initialValue() {
return "The Code Data";
}};
System.out.println(threadLocal.get());
threadLocal.set("The Code Data1");
System.out.println(threadLocal.get());
threadLocal.remove();
System.out.println(threadLocal.get());
}
}
Transaction ID Generation with ThreaLocal
package ThreadClass;
public class Transaction extends Thread {
static Integer transactionId = 0;
private int amount;
private static ThreadLocal threadLocal = new ThreadLocal() {
@Override
protected Object initialValue() {
return ++transactionId;
}
};
public Transaction(int amount) {
this.amount = amount;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "Executing with Transaction Id :" + threadLocal.get());
}
}
class TransactionMain{
public static void main(String[] args) {
Transaction transaction1 = new Transaction(100);
Transaction transaction2 = new Transaction(200);
Transaction transaction3 = new Transaction(300);
Transaction transaction4 = new Transaction(400);
transaction1.start();
transaction2.start();
transaction3.start();
transaction4.start();
}
}
In the above Java program for every transaction, a separate transaction id is managed by the ThreadLocal object .
Similar Java Tutorials
- Odd Even in Java
- Method to print exception information in Java
- Exception Handling by Using try-catch in Java
- Checked Exception vs Unchecked Exception
- Exception Hierarchy in Java
- Java Exception Handling Interview Questions
- final finally finalize in java
- User Defined Exception in Java
- Exception Handling Keyword in Java
- throw throws in Java
- try with multiple catch block in Java
- Threads in Java
- Thread priority in Java
- Threads communication in Java
- Daemon Thread in Java
2 thoughts on “Thread Local Class in Java”