Java Concurrency Programming (12) thread-Safe delegation

Source: Internet
Author: User

If each component is already thread-safe in a composite object, it needs to be added with an additional "thread-safe layer", depending on the situation.

Final can modify an attribute that is not copied, as long as it is assigned to a static code block or constructor.

Stand-alone state variables

We can also delegate the security of threads to multiple state variables, as long as they are independent of each other, that is, the combined class does not add any invariants to the multiple state variables it contains.

If there are some invariance conditions between the state variables, it is not so simple. For example, there are two properties in a class, and the condition to be satisfied is that the value of one property is less than the value of another. This is typically done after the first check, if there is not enough locking mechanism to ensure the atomicity of the operation, this delegation is not enough to achieve the security of the thread. In this case, each class must provide its own locking mechanism to ensure that these compound operations are atomic operations.

A variable can be declared volatile only if it participates in an invariant condition that contains other state variables.

For example, to return the coordinates of a point, you need to get the value of the coordinate x, and the value of the coordinate y, which may result in a different value: The point never appears on this coordinate.

Java Concurrency Programming (12) thread-Safe delegation

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.