1, if more than one thread accesses the same mutable state variable when not using the appropriate synchronization, then the program will be an error, there are three ways to fix the problem:
- This state variable is not shared between threads.
- Modify the state variable to a variable that is immutable.
- use synchronization when accessing state variables.
2. What is thread-safe?
- This class is thread-safe when multiple threads access a class, regardless of how the runtime environment is scheduled or how those threads will be executed alternately, and if no additional synchronization or collaboration is required in the keynote code, and the class behaves correctly.
- Simpler definition: When multiple threads access a class, the class always shows the correct behavior, then it is called a thread-safe class.
3, the thread security class encapsulates the necessary synchronization mechanism, so the client does not need to take further synchronization measures.
4.
Java Concurrency Programming Combat: Note one