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