Combination of objects in Java concurrency programming practice

Source: Internet
Author: User

---restore content starts---

1 purpose of the object combination

The combination of some existing thread-safe classes into larger components is also thread-safe;

1.1 The class that constructs the thread safety needs to consider:

All variables that make up the state of the object;

The invariant condition of the object variable (the object variable has the range of values, to ensure that the value of the variable is within this range,

Otherwise, the state of the object is invalid);

Establish the Concurrency management strategy of object State;

1.2 Categories of invariance conditions and post-condition of classes

Invariant conditions: Variables have a range of values, such as long Count,count value range is long.min_value,long.max_value,

Coupled with their own definition of count>=0;, the invariant condition of the count variable is count>=0 && count <= long.max_value;

Posterior condition: Determines whether the object from a state A to State B is valid;

1.3 Ownership of the state

The object encapsulates the state it owns, the ownership of the State, i.e. the ability to change the state, and if the object's reference is published, then the object is not the ownership of the exclusive state, and it needs to share the ownership of the state with the others;

    

---restore content ends---

1 purpose of the object combination

The combination of some existing thread-safe classes into larger components is also thread-safe;

1.1 The class that constructs the thread safety needs to consider:

All variables that make up the state of the object;

The invariant condition of the object variable (the object variable has the range of values, to ensure that the value of the variable is within this range,

Otherwise, the state of the object is invalid);

Establish the Concurrency management strategy of object State;

1.2 Categories of invariance conditions and post-condition of classes

Invariant conditions: Variables have a range of values, such as long Count,count value range is long.min_value,long.max_value,

Coupled with their own definition of count>=0;, the invariant condition of the count variable is count>=0 && count <= long.max_value;

Posterior condition: Determines whether the object from a state A to State B is valid;

1.3 Ownership of the state

The object encapsulates its state, owning the state ownership, which is the ability to change the state, and if the object's reference is published, it is not the ownership of the object's exclusive state, but the ownership of the shared state with the other;

2. Instance closure

  Object A is enclosed in another object B, and the path to access object A is known to facilitate thread safety, and if the data is enclosed in an object, access to the data is done through methods, making it easier to ensure that the correct lock is taken when concurrency occurs;

Combination of objects in Java Concurrency programming practice

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.