I'm developing a Java Spring MVC project and i hesitate using instance variable in Java Spring Bean.I want to ask some question about this subject.
I used a instance variable in my Java Spring Bean and its type is String.
private String abc = "hell";
As we know , Java Spring default scope is Singleton and they are constructed at the startup time of the project. .It is single instance and my instance variable must be thread-safe.
I used this variable "abc" in the methods of the beans and when they are reached by multiple threads will it damage the consistency of data each thread?
For an example , Thread 1 reach the bean and take the abc variable changed it as "hello".On that time , Thread 1 reference to abc variable point "hell" or "hello"? I'm confused at this topic.
I make the String abc variable to ThreadLocal object to provide each thread holds their own abc variable.But i have read that using ThreadLocal objects occurs memory leakage in the Tomcat.After 7.0 version of Tomcat it is said that it is fixed.
Because each thread holds their own variable, and when their job is finished they return the thread pool which is managed by container.But,returning the pool , ThreadLocal object is not destroyed and they cause memory leakage.
Moreover, String object is immutable so in theorical view does it cause the multi-threaded problem ??
is each thread holds their String variable on its own? For example, Thread 1 triggers and then start the method invocation , and create seperate String variable "abc" and Thread 2 triggers then it creates new String variable "abc" and do they damage "abc" variable each other?
I really wonder this concept of usage and eager to learn the answers.