Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Hi Pankaj, I want to tell you, I follow your posts, I study them. But this post example is not good one. Can you come up with some better example to explain about ThreadLocal class. Thanks in advance!
- Pankaj Chopra
One interesting point about ThreadLocal variable is the global access. Any thread local variable is global to the thread. It can be accessed anywhere from the thread. If, from a thread several methods residing in different classes are called, thread local variable will be visible to all those methods. There is no need to pass the thread local variable as a parameter. At the same time any thread local variable is local to a thread. If there are 10 threads spawned all the 10 threads will have their own thread local variable. One thread can not access/modify other thread’s Thread Local variables.
- infoj
Hi sir, can u give some instances where this could be practically used.I mean wait and notify could be used for Consumer-Producer problems,similarly,where is this concept generally used.
- Sriharsha
Very good article. Below are my comments : This is not required and not in use. protected SimpleDateFormat initialValue() { return new SimpleDateFormat(“yyyyMMdd HHmm”); } Instead this is required : formatter.set(new SimpleDateFormat(“yyyyMMdd HHmm”));
- yogi g
I got the point that ThreadLocal maiitains a private kind of value respective to the threads. But in other sites too,it is given that it could be used as an alternative to synchronization,to improve performance. I just wonder,since we creating a new value(object instance) in its initial value,obviously,all the changes made to that object by that thread will be exclusive to that object only.So maintaining “thread safety”.I could get this even with Out ThreadLocal variable or Synchronized block,if i am creating a new Instance for every thread. Could some one please explain, what is special in this ThreadLocal for making its things private,isn’t it obvious? Couldn’t i get same thing,by making separate objects,without ThreadLocal and achieve this thread safety?
- Sriharsha
I’m try to understand how to use the ThreadLocal class. I understand that there is a cleanup phase but this article doesn’t mention anything about it !
- Muhammad Gelbana
The lambda expression part at the end, why do you need the “” in front of “withInitial”? I can just call ThreadLocal.withInitial(…), right? Thanks for the great articles!
- dz
What I do not see is where ThreadLocalExample that implements Runnable starts … Thank you
- Andrey