I. Designing Thread-safe classes
- Find all variables that construct the state of the object (if the variable is a reference type, also includes a field in the Reference object)
- Invariant conditions of constrained state variables
- Establish concurrent access management policies for object state (how to maintain thread safety)
1. Collect synchronization requirements (find compound operations, multiple variables follow atomic operations, etc.)
2, Dependent State operation (find out if the operation is based on a priori condition, example: remove when the queue is not empty)
3. Ownership of the state (which threads the object is owned by, and which threads can manipulate the object)
Ii. Closure of instances
Encapsulating data inside an object allows data access to be restricted to the methods of the object, ensuring simple and correct holding of the lock. (because you don't need to observe the entire program, just check the current class)
Java provides wrapper to provide thread security for container objects such as ArrayList, HashMap, etc.
1. Java Monitor mode: encapsulates all mutable states of an object and uses the object's built-in lock to protect
Advantages of using a private lock instead of a built-in lock: (private Final Object myLock = new Object ();)
- A private lock encapsulates the lock and the customer code cannot be locked.
- The customer can access the lock through public methods to participate in the synchronization strategy.
2. Example: Vehicle tracking based on monitor mode
The mutablepoint is not necessarily thread-safe, but the class must be thread-safe because it contains both the map object and the point object in the map that have not been published and satisfy the instance closure.
Iii. delegation of thread safety
Classes made up of multiple thread-safe classes are not necessarily thread-safe
Delegate: The thread security of the delegated class is judged by the thread security of the delegate class
1. Delegate to a single thread-safe state variable to ensure thread safety
2, entrusted to a number of mutually independent thread security state variables can guarantee thread security
3, if the class contains multiple thread-safe state variables of the compliance operation, there is no guarantee of thread security, can be guaranteed by locking mechanism
4, Release: If a state variable is thread-safe, and there is no invariant condition constraint it (example: greater than 0), there is no disallowed state transition on the variable operation, it can be safely published
5. Secure release of the thread-safe class of the underlying state
1 @ThreadSafe2 Public classPublishingvehicletracker {3 Private FinalMap<string, safepoint>locations;4 Private FinalMap<string, safepoint>Unmodifiablemap;5 6 PublicPublishingvehicletracker (map<string, safepoint>locations) {7 This. Locations =NewConcurrenthashmap<string, safepoint>(locations);8 This. Unmodifiablemap = Collections.unmodifiablemap ( This. locations);9 }Ten One PublicMap<string, safepoint>getlocations () { A returnUnmodifiablemap; - } - the Publicsafepoint getLocation (String id) { - returnlocations.get (ID); - } - + Public voidsetlocation (String ID,intXinty) { - if(!Locations.containskey (ID)) + Throw NewIllegalArgumentException ("Invalid vehicle name:" +ID); A locations.get (ID). set (x, y); at } -}
Iv. adding functionality to existing thread-safe classes
Example: adding "if not added" function in vector, you can use the extended method, bettervector inherit to vector and increase atomicity through synchronization mechanism for the conforming operation.
1, the client lock mechanism
Using the code of an object must use the lock that the object itself uses to protect its state, not recommended (the synchronous implementation is divided into two unrelated classes)
When the same lock is not used, it is insufficient to provide thread security:
2. Combination
Improvedlist delegates the operation of the list to the underlying list instance and adds an extra lock with its own built-in lock, adding a new synchronization method.
V. Document the synchronization Strategy
The fourth chapter: Object combination--java Concurrent programming combat