012 thread safety and lock optimization

Source: Internet
Author: User

1. Thread safety In the Java language is sorted according to the "security degree" of thread safety from strong to weak, and the data shared by various operations in the Java language can be divided into the following 5 classes: immutable, absolute thread-safe, relative thread-safe, thread-compatible, and thread-antagonistic.

2, thread-safe implementation method ① mutex synchronization
Mutual exclusion Synchronization (Mutual exclusion&synchronization) is a common method of concurrency correctness assurance. Synchronization is when multiple threads concurrently access shared data, ensuring that shared data is used by only one (or some, use of semaphores) threads at the same time. While mutual exclusion is a means of realizing synchronization, the critical section (Critical), mutex (mutex) and semaphore (Semaphore) are the main mutually exclusive implementations.
In Java, the most basic mutex synchronization means is the Synchronized keyword.

The main problem with mutex synchronization is the performance issues caused by thread blocking and wake-up, so this synchronization is also known as blocking synchronization (Blocking synchronization).
② non-blocking synchronization optimistic concurrency strategy based on conflict detection, in layman's words, is the first operation, if no other thread contention for shared data, the operation succeeds, if there is contention for the shared data, there is a conflict, then take other compensation measures (the most common compensation is to continue to retry until successful), Many implementations of this optimistic concurrency policy do not need to suspend threads, so this synchronization operation is called non-blocking synchronization (non-blocking synchronization).
③ no synchronization scheme to ensure thread safety, it is not necessarily necessary to synchronize, there is no causal relationship between the two. Synchronization is only a means of ensuring the correctness of shared data contention, and if a method does not involve sharing data, it naturally does not require any synchronization to ensure correctness, so some code is inherently thread-safe:
    • Reentrant code: This code, also known as pure Code, can break it at any moment of execution of the code, instead of executing another piece of code (including recursively invoking itself), and the original program will not have any errors after control returns. reentrant In contrast to thread safety, reentrant is a more basic feature that guarantees thread safety, that is, all reentrant code is thread-safe, but not all thread-safe code is reentrant.
Reentrant code has some common characteristics, such as not relying on the data stored on the heap and the common system resources, the amount of state used by the parameters passed in, not calling non-reentrant methods, and so on.
    • Thread native Storage (thread local Storage)

3, lock optimization such as adaptive Spin (Adaptive Spinning), lock elimination (lock elimination), lock coarsening (lock coarsening), lightweight lock (lightweight Locking) and biased lock (biased Locking) and so on, these technologies are designed to increase the efficiency of program execution by sharing data more efficiently between threads and resolving competition issues.


From for notes (Wiz)

012 thread safety and lock optimization

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.