Friends
All the threads of Object in java share same variables and if that variable is not thread safe , we can use synchronized , we have to handle this situation as multiple threads share same variable. to avoid this either we have to go for synchronization or make the variable ThreadLocal , these variables are private statis and accessible to only that thread . we can provide setter and getter to access this variable.
Short Notes :
---------------
"Java ThreadLocal is used to create thread local variables. We know that all threads of an Object share it’s variables, so the variable is not thread safe. We can use synchronization for thread safety but if we want to avoid synchronization, we can use ThreadLocal variables."
Comments