Instance variable in Java Spring Bean

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.

+4
source share
2 answers

I will try to resolve your issues separately.

I used this abc variable in beans methods and when they are reached by multiple threads, will it harm the data consistency of each thread?

The short answer to this question is yes. If you wrote a different value for abc in one Thread , you may or may not see from another thread. For this reason, the volatile keyword was introduced.

I want mutable singleton beans in Spring , then you need to be careful with synchronizing and using volatile .

The data will not be “corrupted” because, as you indicate, String is immutable. But one thread may not see another change the value of abc .

So you have the variable abc ;

  • abc = "Hello."
  • thread 1 reads abc as "Hello." .
  • thread 1 modifies abc by writing to the link: abc="Hello."
  • thread 2 reads abc , it is unclear whether abc reads "Hello." or "hello." .

I am making an abc String variable to a ThreadLocal object so that each thread contains its own abc variable

ThreadLocal used so that each thread is given a different instance of the variable. This is often used to bind state to a thread on a web server, as web servers use a pre-stream request model.

Tomcat 7 does not fix ThreadLocal leak problems. If you do not delete the variable specified from the stream, it will cause a leak. Or, even worse, the request will receive items from the context of another request. This happens when a thread is checked in the pool with the context remaining in its ThreadLocal , and then checked by another request.

The Tomcat 7 buses detect these problems and replace threads to reset their ThreadLocal variables. It is expensive and can be avoided by using ThreadLocal correctly.

You need to clear the ThreadLocal variables in the servlet filter.

So:

If you have an abc variable that you want to write to in one thread and you have not seen the change in other threads for this, you can use ThreadLocal , but you need to be very careful to clear the state after each request.

If you have the abc variable that you want to write to in one thread, and for this change , which will be visible in other threads, you can use a singleton bean with volatile .

+4
source

I think, in your case, if you define the area of ​​your bean that should have your instance variables as Singleton .

According to the Spring reference document, the following is a definition for an area with bean syntax.

 To put it another way, when you define a bean definition and it is scoped as a singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object. 

Singleton is the default area in which your bean will be placed if you do not specify.

In addition, the statement below should be kept in mind if you try to handle your case using the Spring bean approach.

 This approach is powerful and flexible in that you can choose the scope of the objects you create through configuration instead of having to bake in the scope of an object at the Java class level. 
+1
source

Source: https://habr.com/ru/post/1495178/


All Articles