How Java concurrency adds new functionality to existing thread-safe classes--java concurrent programming practices

Source: Internet
Author: User


The safest way to add a new atomic operation is to modify the original class to support the desired operation. However, you may not have access to the source code or the freedom to modify it, so it is usually impossible. Even if you can modify the original class, you also need to understand its implementation of the synchronization strategy, in order to maintain the original design of the premise to improve its functionality. Adding a new method directly to a class means that all code that implements the class synchronization policy is still contained in a source code file, so it is easy to understand and maintain.

Another way is to extend this class . Because the implementation of the synchronization policy is distributed across multiple independently maintained source code files after the extension, it is more vulnerable to extend an analogy directly into the class. If the underlying class chooses a different lock to protect its state variable, which alters its synchronization strategy, the subclass is unknowingly destroyed because it can no longer use the correct lock to control concurrent access to the base class state.

The third strategy is client-side locking, which extends the functionality, rather than extending the class itself, and placing the extension code in an "Assistant" class.

Code examples and precautions are described in P72.

The fourth choice is also a more robust choice: combination.


How Java concurrency adds new functionality to existing thread-safe classes--java concurrency programming practices

Related Article

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.