Concurrent Programming-object combination and concurrent programming

Source: Internet
Author: User

Concurrent Programming-object combination and concurrent programming
Object combination 1. Design thread-safe classes

1. In the process of designing the thread security class, it should include the following three basic elements:

1) Find all the variables that constitute the object state

2) Find Out immutable conditions for variable Constraints

3) Establish concurrent access management policies for object states

2. Collect synchronization requirements

To ensure the thread security of the class, it is necessary to ensure that its immutability conditions will not be damaged when concurrent access is performed, and thus the State of the class needs to be inferred.

Both objects and variables have a state space, that is, the smaller the state space for all possible values, the easier it is to judge the state of the thread.

If you do not understand the immutability and posterior conditions of an object, thread security cannot be guaranteed for a long time. Atomicity and encapsulation are required to meet various conditions on the Number of valid values or state transition.

3. Dependency operations

If an operation contains a prior state-based condition, this operation is called a dependent state operation.

Ii. instance closures

1. encapsulation simplifies the implementation process of the thread security class. It provides an instance blocking mechanism, which is generally referred to as "closed ".

2. encapsulate data inside the object. You can restrict data access to the object method, so that it is easier to ensure that the thread can always hold the correct lock when accessing data.

3. The blocking mechanism is easier to construct thread security classes, because when the status of the closed classes changes, the entire program does not need to be checked during thread security analysis.

4. Java monitor mode

The Java monitor mode can be derived from the thread blocking principle and its logical inference. Objects that follow the Java monitor mode encapsulate all the variable states of the object and are protected by the built-in locks of the object.

Iii. Delegate thread security

If a state variable is thread-safe and does not have any immutability conditions to constrain its value, there is no allowed state conversion in variable operations, this variable can be safely released for a long time.

 

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.