Concurrent Programming-object combination and concurrent programming
Object combination 1. Design thread-safe classes
1. In the process of designing the thread security class, it should include the following three basic elements:
1) Find all the variables that constitute the object state
2) Find Out immutable conditions for variable Constraints
3) Establish concurrent access management policies for object states
2. Collect synchronization requirements
To ensure the thread security of the class, it is necessary to ensure that its immutability conditions will not be damaged when concurrent access is performed, and thus the State of the class needs to be inferred.
Both objects and variables have a state space, that is, the smaller the state space for all possible values, the easier it is to judge the state of the thread.
If you do not understand the immutability and posterior conditions of an object, thread security cannot be guaranteed for a long time. Atomicity and encapsulation are required to meet various conditions on the Number of valid values or state transition.
3. Dependency operations
If an operation contains a prior state-based condition, this operation is called a dependent state operation.
Ii. instance closures
1. encapsulation simplifies the implementation process of the thread security class. It provides an instance blocking mechanism, which is generally referred to as "closed ".
2. encapsulate data inside the object. You can restrict data access to the object method, so that it is easier to ensure that the thread can always hold the correct lock when accessing data.
3. The blocking mechanism is easier to construct thread security classes, because when the status of the closed classes changes, the entire program does not need to be checked during thread security analysis.
4. Java monitor mode
The Java monitor mode can be derived from the thread blocking principle and its logical inference. Objects that follow the Java monitor mode encapsulate all the variable states of the object and are protected by the built-in locks of the object.
Iii. Delegate thread security
If a state variable is thread-safe and does not have any immutability conditions to constrain its value, there is no allowed state conversion in variable operations, this variable can be safely released for a long time.