4.1 Designing Thread-safe classes
By using encapsulation technology, you can determine whether a class is thread-safe without analyzing the entire program.
In the process of designing a thread-safe class, it is necessary to include the following three basic elements: to find out all the variables that make up the state of the object, to find the invariant condition of the constraint state variables, and to establish concurrent access management policies for the object state.
To parse the state of an object, start with the object's domain first.
Synchronization policies (synchronization Policy) define how access to their state can be coordinated without violating object invariant or posteriori conditions. The synchronization policy specifies how immutability, thread closures, and locking mechanisms can be combined to maintain thread safety, and which variables have locks to protect.
4.1.1 Collecting synchronization requirements
To ensure thread security for a class, you need to make sure that its invariant conditions are not compromised in the concurrency situation, which requires an inference of its state. Both the object and the variable have a state space, which is all possible values. The smaller the state space, the easier it is to determine the state of the thread. The more you use a final type of domain, the more you can simplify the parsing of the possible state of the object.
4.1.2 Dependent State operation
The invariant and posterior conditions of a class constrain what states and state transitions are valid on an object. If a priori condition based on a state is included in an operation, then this operation becomes a dependent state operation.
Ownership of the 4.1.3 State
In many cases, ownership and encapsulation are interrelated: the object encapsulates the state it owns, and vice versa, that is, ownership of the state it encapsulates.
The container class typically shows a form of "ownership separation" where the container class has its own state, while the customer code owns the state of each object in the container.
Java concurrency Combat the combination of the fourth chapter object