The combination of the fourth object of Java concurrent Programming practice reading notes

Source: Internet
Author: User
Tags set set


One, the design of thread-safe classes in the design of the thread-safe class, the need to include the following three basic elements:
. Find all the variables that make up the state of the object.


. Find the invariant condition of constraint state variable.
. Establish a concurrent access management policy for object state.
Parses the state of an object, starting with the domain of the object. Variables are divided by scope:. Global variables. Local variables. The method line participates. Exception Handling Parameters

1. Collecting synchronization requirements assuming that the invariant and posterior conditions of the object are not understood, thread safety cannot be ensured. To satisfy the various constraints on a valid value or state transition of a state variable. Requires the use of atomicity and encapsulation.

To be more brief is that the Java line Shuo all because of shared variables, shared variables will be caused by multiple threads at the same time cause the wrong problem, so the collection of a common number of places will involve these need to synchronize the variables, There are only a few things that can be said to be problematic. Based on this, it ensures that all elements are thread-safe and that the program is thread-safe.
2. The operation prior condition of a dependent state is the ability to process a value after it satisfies a certain condition. For example: first infer whether a queue is empty, assuming null.

。。

, assuming that it is not empty.

。 It is a priori condition to infer whether the queue is empty.


Suppose you include a priori condition based on a state in an operation. Then this operation is called a dependent state operation.
? Is it possible to meet visibility?
3. All rights of State a single base object is more secure, but the assumption is to include a collection of objects (container classes such as: ArrayList), the container class usually shows a form of "all right separation". Even with a thread-safe container Class (Collections.synchronizedlist (list<t>)), it is only possible to ensure that container-related operations are thread-safe, assuming that a reference to a Mutable object is advertised. Will not have exclusive control over it. (Non-thread safe)

The instance encapsulation encapsulates the data inside the object, allowing access to the data to be restricted to the method of the object, making it easier to ensure that the thread always holds the correct lock when visiting the data.
A closed mechanism is easier to construct a thread-safe class, because when the state of the enclosing class is parsed, the entire program is not required to examine the thread security of the class.


Even if the closure can guarantee that all processing within the object is readily available, it is also important to note that when the object is advertised, it is possible to fail, such as hashset<persion>, in addition to ensuring that persion is thread-safe, it is also necessary to ensure that the set set used by it is thread-safe.

-only one variable in the sample object ensures that the variable is thread safe. Using synchronized on a method
1. Java Listener mode synchronized by specifying object lock
2. Demo Sample: Vehicle Tracking-Ensure container class instances, container classes including element instance thread safety.

Third, the thread security entrusted. How to guarantee the collection class and its inclusion class thread security
. Adding functionality to existing thread-safe classes is inherited from the current collection class, and locking allows the method to be added to ensure security, but this is more fragile. Combination, view book source code
. Document the synchronization policy

The combination of the fourth object of Java concurrent Programming practice reading notes

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.