Whether an object needs to be thread-safe depends on whether it is accessed by multiple threads .
When multiple threads access the same variable state amount , it is possible to make an error if the correct synchronization rules are not used. Workaround:
- Do not share this variable between threads
- Modify the state variable to be immutable
- Using the synchronization mechanism when accessing state variables
Programs that are constructed entirely by thread-safe classes are not necessarily thread-safe, and can also contain non-thread-safe classes in thread-safe classes
First, what is thread safety
thread safety is when multiple threads are accessing a class, and if no additional synchronization is required, the behavior of the class is still correct. (because the necessary synchronization code is encapsulated in the thread-safe class)
a stateless class is thread-safe. a stateless class is a field that does not contain any fields or references other classes. The instantaneous state of a particular calculation, which is uniquely present in the local variable.
Second, the atomic nature
1, competition conditions: due to improper implementation of the time when the incorrect results appear, when the correctness of the calculation depends on the order of the cross-execution
Reason for error: Next step based on an observation that may fail
Example 1: Read-Modify-write operation: When two threads cross and read the same value simultaneously, the AddOne operation results in a deviation-1 result.
1 public class Test {2 private int x = 0;3 public void AddOne () {4 x++;5 }6}
Example 2: A single-piece mode that does not perform synchronous operation after first detection:
2. Compound operation
Concept: Contains a set of actions that must be performed atomically to ensure thread security
Atomic operations are thread-safe vs. competitive conditions are unsafe
Adding a state that ensures thread safety in a stateless class guarantees that the class is still not thread-safe, but there is no way to ensure that it is joined in multiple thread-safe states.
Third, locking mechanism
1. Built-in Lock: Java provides a built-in locking mechanism that enforces atomicity: synchronized blocks, each Java object can be used as a lock, called a built-in lock or a monitoring lock
- Synchronized (lock) {Access or modify a shared variable}
- Synchronized method to ensure that the entire method body is atomic, lock is this
- Static Synchronized method with class object as lock
At most one object holds the lock, enters the synchronization code block to obtain the lock, and the code block releases the lock
The synchronized attribute cannot be inherited, that is, the Overwrite method needs to also write the Synchronized keyword
2. Re-entry
A built-in lock can be re-entered, that is, a thread can obtain a lock it already holds. (an example of the same-step class method calls the parent class method Super.method () requests the lock it already has)
The JVM provides a lock thread counter to ensure the correct release of the lock
Iv. using locks to protect objects
Locks enable the protected code to be executed in a serial form.
For a state variable that may be accessed by multiple threads, it is locked when it is accessed, not just when it is written.
Each shared or mutable state should be protected by a lock
The lock mechanism only prevents other threads from acquiring the same lock, and does not prevent access to the object
Even though each method uses a synchronization mechanism, the composite operation made up of them does not necessarily guarantee concurrency to be correct.
Cases
1 public Add (e e) {2 if (!vector.contains (e)) 3 Vector.add (e); 4}
V. Activity and Performance
If the entire services method is locked, and there is a lengthy calculation in the method, it will seriously affect the efficiency
After modifying the code, the green Arrow refers to the large computational amount of partial resynchronization code to remove, can be concurrent operation, improve performance
Java Concurrency Programming Combat: Chapter Two----thread safety