ThreadLocal variable in JAVA with Example

This guide will help you understand about ThreadLocal variable and how to create and access the ThreadLocal variable in Java.

You will learn:
– What is the ThreadLocal variable?
– Why do you need the ThreadLocal variable?
– How to create and access a ThreadLocal variable?

Few more Concurrent related articles from Codedelay

ConcurrentHashMap with example.
CompletableFuture Examples

What is the ThreadLocal variable in JAVA?

The ThreadLocal variable allows you to store data in the execution context of each thread.

Thus each thread can have its own instance of data that can only be read and written by the same thread.

Benefits of using ThreadLocal variable

  • A ThreadLocal variable is essentially a container that contains a copy of the variable for each thread created from a class. Therefore, other threads can’t read and write data from that ThreadLocal variable.
  • Apart from synchronization, ThreadLocal is another way to achieve thread-safety in Java.

How to create a ThreadLocal variable

To create a ThreadLocal object, we can use the no-arg constructor of the ThreadLocal class.

In the above example, I have created threadLocal variable which will store string value.

Once you have created the instance of ThreadLocal class, you can set the value using set() method.

To value the value you can call get() from the threadLocal reference variable.

Now let me show you how the final code looks like.

ThreadLocal as an alternative option to synchronization

ThreadLocal is also useful when you want to avoid synchronization due to performance cost.

Let’s take an example of SimpleDateFormat.

SimpleDateFormat is not a thread-safe class that means in a multithreaded environment data inconsistency is the challenge with SimpleDateFormat.

In the above example, you may notice that DateFormat is not thread-safe.

Therefore, we have added a synchronized block to avoid data inconsistency issues.

Let’s change the implementation using ThreadLocal.

The output of the above program would be:

In the above program, you might have noticed that we have overridden initialValue() to set date.

Let’s take a look at the initialValue() method of ThreadLocal class.

The initial value of a ThreadLocal variable

In order to provide the initial value to the ThreadLocal variable, we can use the initialValue() method provided by the ThreadLocal class.

You can also use the lambda expression to simplify the above code block.

Once you provide an initial value, all the threads will the same value when they call get() before set()

Conclusion

Apart from synchronization, ThreadLocal in Java is an alternative way to provide thread-safety.

ThreadLocal lets each thread to have its own local variable. We can also initialize ThreadLocal values using the initialValue() method.

3 thoughts on “ThreadLocal variable in JAVA with Example”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.