ThreadLocal class in Java

ThreadLocal is a class in Java which enables to keep the different copies for each thread. In more general terms lets say thread t1 modified ThreadLocal class variable String from one value to another value this change will not be visible to the other threads although they read the same variable after the change.

This is another way of achieving the thread safety in java.

ThreadLocal example in Java

output of the above program as follows

If we see the above output, all the threads are sharing the same ThreadLocal variable although each thread have its own copies