Deep understanding of Java Virtual Machine-Chapter 2-thread security and lock optimization, and Chapter 4

Source: Internet
Author: User

Deep understanding of Java Virtual Machine-Chapter 2-thread security and lock optimization, and Chapter 4

Chapter 4 thread security and lock Optimization

 

Thread Security

Thread security in java

1 is unchangeable. The Immutable object must be thread-safe.

2. Absolute thread security: no matter what the runtime environment is, the caller does not need any additional synchronization measures. Generally, the caller must pay a large or even unrealistic price, mark itself as a thread-safe class in java APIs. Most of them are not absolute thread-safe.

3. Relatively thread-safe

4. The thread-compatible object itself is not thread-safe. However, you can ensure that the object can be safely used in the concurrent environment by correctly using Synchronization Methods on the caller.

5. Opposition to threads

Implementation of thread security

1 mutex Synchronization Murual Exclusion & Synchronization

A common mobile phone with concurrent correctness is guaranteed. Synchronization means that when multiple threads concurrently access Shared data, the shared data is saved and used by only one thread at a time.

The most basic mutex is the synchronized keyword.

You can also use the lock in the java. util. concurrent package to implement synchronous ReentrantLock,

JDK1.6 optimized synchronized, and the efficiency was basically the same as that of ReentrantLock.

2. Non-blocking Synchronization

Optimistic Concurrency strategy based on Conflict Detection

CAS operation Compare-and-Swap

3 No synchronization Solution

Lock Optimization

1. Spin locks and adaptive spin

2. Lock Elimination

3. Lock roughening

4. Magnitude lock

5. Biased lock

 

The entire book can be viewed in the Blog: deep understanding of Java Virtual Machine (VM) 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.