How to make static methods thread safe in Java?

I'm creating a web application and I'm experiencing thread safety issues After reading several similar questions, I am still confused about my case I am using the Java spring framework to set up rest web services All requests (JSON of the person object) will be passed to the checkifgoodname function, such as checker checkIfGoodName(person). They are all static method calls I want to know, it's this function, checker checkIfGoodName THREAD SAFE? If not, how to modify the code? My code is as follows:

Checker. java

public class Checker {

    public static void checkIfGoodName(Person person){

        checkingName(Person person);

    }

    private static void checkingName(Person person){

        if(person.getName()==null){
            PersonUtils.addErrorMessage(Person person,new String("Name is empty"));
        }

    }

}

PersonUtils. java

public class PersonUtils {

     public static void addErrorMessage(Person person,String errorMessage){

         List<Message> msg = person.getMessageList();
         if(msg!=null){
             msg.add(buildMessage(errorMessage));
         }
     }

     public static void buildMessage(String errorMessage){
         if(errorMessage != null){
             Message msg = new Message();
             msg.setMsg(errorMessage);
         } 
     }

}

Solution

Do not consider thread - safe methods Thread safety is about protecting data integrity More specifically, it prevents threads from accessing data while some other threads are changing data

PersonUtils. The adderrormessage (person, message) method modifies the list instance belonging to the person instance Access to the list should be synchronized if the same list can be modified by two different threads, or it can be modified by one thread and accessed by other threads

Adding an item to the list takes several steps. If thread a can see it at some point when thread B performs some but not all steps, the list will almost certainly look illegal If two threads try to modify the list at the same time, it is even worse: this may leave the list in a permanently illegal state

Even if the threads running on the same person instance do not actually execute at the same time, you still need to synchronize The reason is that if there is no synchronization, the computer hardware and operating system do not guarantee that changes made by one thread in memory will be immediately visible to other threads But sync to save you:

After thread B enters the synchronization (foo) block, any changes made by thread a before leaving the synchronization (foo) block will be visible to thread B

If different threads access the same person instance, the simplest thing is to synchronize the person object in the adderrormessage (...) method:

public static void addErrorMessage(Person person,String errorMessage){
      synchronized(person) {
          List<Message> msg = person.getMessageList();
          if(msg!=null){
             msg.add(buildMessage(errorMessage));
          }
      }
  }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>