2nd Chapter Thread Safety

Source: Internet
Author: User

When building robust concurrent programs, threads and locks must be used correctly. But these are just some mechanisms. The core of writing thread-safe code is to manage state access operations, especially for shared and variable (Mutable) state access.

In the informal sense, the state of an object is the data stored in a state variable (for example, an instance or a static variable). The state of the object may include other dependent objects for the domain.

"Sharing" means that variables can be accessed concurrently by multiple threads, while "mutable" means that the value of a variable can change over its lifetime. Whether an object needs to be thread-safe depends on whether it is accessed by multiple threads. This refers to the way the object is accessed in the program, not the functionality that the object implements. To make an object thread-safe, you need to use a synchronization mechanism to coordinate access to the mutable state of the object. Failure to achieve synergies can lead to data corruption and other results that should not occur.

When multiple threads access a state variable and one of the threads performs a write operation, the synchronization mechanism must be used to coordinate the access of the variables to those threads. The primary synchronization mechanism in Java is the keyword synchronized, which provides an exclusive lock locking method, but the term "synchronization" also includes volatile type variables, display locks (Explicit Lock), and atomic variables.

If you do not use the appropriate synchronization when multiple threads access the same mutable state variable, the program will have an error and there are three ways to fix the problem:
(1) The state variable is not shared between threads;
(2) Change the state variable to an immutable variable;
(3) Use synchronization when accessing state variables;

2nd Chapter Thread Safety

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.