When building robust concurrent programs, threads and locks must be used correctly. But these are just some mechanisms. The core of writing thread-safe code is to manage state access operations, especially for shared and variable (Mutable) state access.
In the informal sense, the state of an object is the data stored in a state variable (for example, an instance or a static variable). The state of the object may include other dependent objects for the domain.
"Sharing" means that variables can be accessed concurrently by multiple threads, while "mutable" means that the value of a variable can change over its lifetime. Whether an object needs to be thread-safe depends on whether it is accessed by multiple threads. This refers to the way the object is accessed in the program, not the functionality that the object implements. To make an object thread-safe, you need to use a synchronization mechanism to coordinate access to the mutable state of the object. Failure to achieve synergies can lead to data corruption and other results that should not occur.
When multiple threads access a state variable and one of the threads performs a write operation, the synchronization mechanism must be used to coordinate the access of the variables to those threads. The primary synchronization mechanism in Java is the keyword synchronized, which provides an exclusive lock locking method, but the term "synchronization" also includes volatile type variables, display locks (Explicit Lock), and atomic variables.
If you do not use the appropriate synchronization when multiple threads access the same mutable state variable, the program will have an error and there are three ways to fix the problem:
(1) The state variable is not shared between threads;
(2) Change the state variable to an immutable variable;
(3) Use synchronization when accessing state variables;
2nd Chapter Thread Safety