Home  >  Article  >  Java  >  What classes in Java are thread safe?

What classes in Java are thread safe?

(*-*)浩
(*-*)浩Original
2019-05-21 13:36:398130browse

To write thread-safe code, the core lies in managing state access operations, especially access to shared and mutable states. When multiple threads access a state variable and one thread performs a write operation, a synchronization mechanism must be used to coordinate the access of these threads to the variable. Stateless objects must be thread-safe.

What classes in Java are thread safe?

What happens if we add a state to a stateless object?

Suppose we add a "hit counter" to the servlet to manage the number of requests in the following way: add a long type field to the servlet, and add 1 to this value every time a request is processed.

public class UnsafeCountingFactorizer implements Servlet {
     private long count = 0;
 
     public long getCount() {
            return count ;
     }
 
     @Override
     public void service(ServletRequest arg0, ServletResponse arg1)
                 throws ServletException, IOException {
            // do something
           count++;
     }
}

Unfortunately, the above code is not thread-safe because count is not an atomic operation. In fact, it contains three separate operations: reading the value of count, adding 1 to the value, and then The calculation result is written into count. If thread A reads that count is 10, thread B immediately reads that count is also 10. Thread A adds 1 and writes to 11. Thread B has already read that count is 10, so after adding 1 and writing, it is still 11. A count is thus lost.

In concurrent programming, this kind of incorrect result due to improper execution timing is a very important situation. It has a formal name: race condition. The most common type of race condition is the "check first and then execute" operation, that is, a possible invalid observation result is used to determine the next operation.

Delayed initialization is a common situation of race conditions:

public class LazyInitRace {
     private SomeObject instance = null;
     public SomeObject getInstance() {
            if(instance == null)
                 instance = new SomeObject();
            return instance ;
     }
}

Contains race conditions in LazyInitRace: First, thread A determines that instance is null, then thread B determines that instance is also null, and then thread A and thread B create objects respectively, so that the object undergoes two processes. An error occurred during initialization.

To avoid static conditions, when a thread modifies a variable, it is necessary to prevent other threads from using this variable in some way, thereby ensuring that other threads can only read and modify the state before or after the modification operation is completed. , rather than in the process of modifying the state.
In the UnsafeCountingFactorizer example, the reason why the thread is unsafe is that count is not an atomic operation. We can use an atomic class to ensure that the addition operation is atomic.

In this way, the class is thread-safe:

 public class CountingFactorizer implements Servlet {
     private final AtomicLong count = new AtomicLong(0);
 
    public long getCount() {
          return count .get() ;
   }
 
    @Override
    public void service(ServletRequest arg0, ServletResponse arg1)
               throws ServletException, IOException {
          // do something
          count.incrementAndGet();
   }
}

AtomicLong is an atomic variable class in the java.util.concurrent.atomic package. It can implement atomic self-increment operations, so it is thread-safe.

Related learning recommendations: java basic tutorial

The above is the detailed content of What classes in Java are thread safe?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn