6 Basic Knowledge Notes one

Source: Internet
Author: User
Tags visibility

Thread Security

The key to writing thread-safe code is to manage state access, especially for shared and mutable states.

Informally, the state of an object refers to the data stored in the state variable. The state of the object may also include the domain of other objects.

A "shared" variable can be accessed by multiple threads at the same time, and "variable" means that the variable can change over its life cycle.

whether an object requires multiple threads depends on whether it is accessed by multiple threads .

If you ensure that multithreading accesses the same mutable state variable, there is no error. There are three ways to solve this problem:

The

does not share the mutable state between threads. Modifies the state variable to an immutable variable. Use synchronization when accessing the state variable. The right way to write a concurrent application is to make the code run correctly first, and then increase the speed of the code. Performance optimization is best done only when performance test results and applications have to improve performance, and test results show that this optimization is actually able to improve performance even when it comes to optimization. The meaning of correctness is that the behavior of a class is exactly the same as its specification. Thread security Definition: This class is thread-safe when multiple threads have access to a class that can always show the correct behavior. Stateless class: The class does not contain any fields and does not contain any references to fields in other classes. Stateless objects must be thread-safe. In concurrent programming, it is a very important situation to have incorrect results due to improper execution timing, which is called the "race condition". The reason for the occurrence of a race condition is that a race condition occurs when the correctness of a calculation depends on the alternate execution time sequence of multiple threads. The types of race conditions are: "First check and then perform" operation. Read-Modify-write operation. How to avoid the occurrence of a race condition problem: You must prevent other threads from modifying this variable in some way when a thread modifies it, ensuring that other threads can read and modify the state only before or after the modification operation is complete, not in the process of modifying the state. In practice, use existing thread-safe objects to manage the state of the class as much as possible. To keep the state constant, you need to update all the related state variables in an atomic operation. A built-in lock is reentrant, so if a thread tries to acquire a lock that is already held by itself, the request succeeds. "Re-entry" means that the action granularity for acquiring a lock is "thread", not "call". Access to synchronization variables requires synchronization at any location, and must be the same lock.
The sharing of objects in access to shared mutable state is required for proper administration. To ensure the visibility of memory write operations between multiple threads, you must use a synchronization mechanism. The correct synchronization is used whenever data is shared across multiple threads. Lock is designed to ensure the visibility and atomicity of variables. The volatile variable is used when and only if all the conditions are met: The write operation on the variable does not depend on the current value of the variable, or can ensure that only a single thread modifies the value of the variable. The variable is not included in the invariant condition with other variables. You do not need to lock the variable when accessing it. Publishing means that an object can be used in code other than the current scope. Publishing an internal state can disrupt encapsulation and make it difficult to maintain invariant conditions. This is called an escape when an object that should not be published is published. Do not allow this reference to escape during the construction process.

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.