I am developing a Java Spring MVC project, and am shy about using an instance variable in the Java Spring Bean. I want to ask a few questions on this.
I used an instance variable in my Java Spring Bean and its type is String.
private String abc = "hell";
As you know, Java Spring is Singleton by default, and they were built during project startup .. This is the only instance, and my instance variable should be thread safe.
I used this "abc" variable in beans methods, and when they are reached by multiple threads, will it harm data consistency in each thread?
For example, Thread 1 reaches a Bean and accepts the variable abc, changing it to "hello". At that time, Thread 1's reference to the abc point variable "hell" or "hello"? I am confused about this topic.
I am making a String abc variable to a ThreadLocal object so that each thread contains its own variable abc. But I read that using ThreadLocal objects causes a memory leak in Tomcat. After version 7.0, Tomcat says it has been fixed.
Because each thread contains its own variable, and when their job is complete, they return a thread pool that is managed by the container. But, by returning the pool, the ThreadLocal object is not destroyed, and they cause a memory leak.
Also, the String object is immutable, so in theory it causes a multithreaded problem?
does each thread contain its own String variable? For example, a Thread 1 trigger fires a method call and creates separate String "abc" variables and Thread 2 triggers, then it creates a new String "abc" variable and does the "abc" variable damage each other?
I am really interested in this concept of use and the desire to know the answers.