Java concurrent programming practice Chapter 4 object combination Reading Notes

Source: Internet
Author: User


I. Design thread-safe classes in the process of designing thread-safety classes, there must be three basic elements:
. Find all variables that constitute the object state.
Find out the immutability condition of the constraint state variable.
. Create a concurrent access management policy for the object status.
The status of the analysis object starts from the object domain. Variables are divided by scope:. global variables. Local variables. Method row parameters. Exception Handling parameters.

1. Collection synchronization requirements if you do not understand the immutability and posterior conditions of the object, the thread security cannot be ensured. Atomic and encapsulation are required to meet various constraints on the valid values or state transition of state variables. To put it more simply, Java thread security is caused by shared variables. After shared variables are modified by multiple threads at the same time, errors may occur, therefore, the number of variables involved in synchronization is collected. Only when the collection says there are possible causes of problems can the program be thread-safe only by ensuring the thread security of all elements.
2. The prior condition of the dependent State operation is that the value can be processed only after a certain condition is met. For example, first judge whether a queue is empty, if it is empty... if it is not empty... The prior condition is used to determine whether the queue is null.
If an operation contains a prior state-based condition, this operation is called a dependent state operation.
? What if the visibility is met?
3. State ownership a single basic object ensures its security. However, if it is a collection of objects (container class for example: ArrayList), the Container class usually shows a form of "ownership separation. That is, use the thread-Safe Container class (Collections. synchronizedList (List ), And only ensure that the container-related operations are thread-safe. If a variable object reference is published, it will not have exclusive control. (Non-thread security)

2. The instance encapsulation encapsulates data inside the object, and the data access can be restricted on the object method, so that the thread can always hold the correct lock when accessing the data.
The blocking mechanism makes it easier to construct a thread-safe class, because when the status of the closed class is used, the entire program does not need to be checked when analyzing the thread security of the class.
Even if the closure feature ensures that all processing in the object is secure, you must note that problems may still occur after the object is published, such as HashSet. In addition to ensuring that the Persion is thread-safe, it is also necessary to ensure that the Set using it is thread-safe. -The example object only has one variable to ensure thread security. Use synchronized ON THE METHOD
1. the Java listener mode synchronized is locked by specifying an object
2. Example: vehicle tracking-Ensure the thread security of the container class instance and the container class containing element instance.

Iii. Delegation of thread security how to ensure the thread security of collection classes and their contained classes
. Add the function in the existing thread security class to inherit from the current collection class. Apply the lock to ensure the security of the added method, but this is more fragile. Combination Method, view the Book source code
. Document the synchronization Policy

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.