Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Local variable in Singleton class

In a singleton class , am having private static ThreadLocal variable. Whether that variable will be shared among users accessing that singleton class , Or won't be shared ?

like image 853
karthick Avatar asked Apr 21 '26 16:04

karthick


2 Answers

Each thread that accesses the variable of singleton class object (via its get or set method) has its own, independently initialized copy of that variable, it woun't be shared and that is the character of ThreadLocal variable.

Documentation

like image 144
Subhrajyoti Majumder Avatar answered Apr 23 '26 18:04

Subhrajyoti Majumder


ThreadLocal is different. SingleTon is different.

ThreadLocal

Even singleton object has different thread local in different thread.

For ex,

A is a singleton reference. A can be shared in multiple threads. So each thread has thread specific thread local value.

Singleton

Only one object exists in JVM at a time.

like image 45
Siva Kumar Avatar answered Apr 23 '26 18:04

Siva Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!