Java concurrent Programming Combat---Chapter II: Thread Safety

Source: Internet
Author: User

The state of an object refers to the data stored in the state variable. The state of the object may include other dependent objects for the domain. For example, the state of HashMap is stored not only in the HashMap itself, but also in many Map.entry objects. The state of the object contains any data that might affect its external visibility.

Sharing means that a variable can be accessed concurrently by multiple threads, which means that the value of the variable may change over its life cycle.

Thread safety: This class is thread-safe when multiple threads are accessing a class, and the class always shows the correct behavior.

This class is thread-safe when multiple threads access a class, regardless of how the runtime environment is scheduled or how the write thread will be executed alternately, and if no additional synchronization or collaboration is required in the keynote code, and the class behaves correctly.

Stateless objects must be thread-safe.

Stateless object: Contains no fields and does not contain references to fields in other classes.

When a state is added to a stateless class, the class is still thread-safe if the state is managed entirely by a thread-safe object.

Race condition

Race condition: From the point of view of multi-threaded communication, when two or more threads are working on shared data, the final result depends on the order in which these threads are executed.

    • The most common type of race condition is: first check and then execute (check-then-act). It is essentially-----to make judgments or perform a calculation based on observations that may fail.
    • Read-Modify-write. A transformation that defines the state of an object based on its previous state.

Race conditions are not always generated, and some inappropriate order of execution is required.

To avoid race condition problems, you must somehow prevent other threads from using this variable when a thread modifies the variable, ensuring that other threads can read and modify the state only before or after the modification operation is complete, not during the modification process.

When multiple variables are involved in a non-deformation condition, the variables are not independent of each other, but the values of one variable constrain the values of other variables. Therefore, when you update a variable, you need to update the other variables simultaneously in the same atomic operation. To maintain state consistency, you need to update all related state variables in a single atomic operation.

For each invariant condition that contains multiple variables, all of the variables involved require the same lock to protect.

Built-in lock

  Each Java object can be used as a lock that implements synchronization, which is called a built-in lock or a monitor lock. The thread acquires the lock automatically before it enters the synchronization code block and automatically releases the lock when it exits the synchronization code block.

The built-in lock for Java is a mutex.

The built-in lock is reentrant, so if a thread view acquires a lock already held by itself, the request succeeds. Re-entry means that the granularity of the acquire lock operation is a thread rather than a call.

Because locks enable their protected code paths to be accessed serially, some protocols can be constructed through locks to achieve exclusive access to shared state.

  

Java concurrent Programming Combat---Chapter II: 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.