Java concurrency Combat the combination of the fourth chapter object

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.