Home > Java > javaTutorial > java virtual machine--thread safety and lock optimization

java virtual machine--thread safety and lock optimization

伊谢尔伦
Release: 2016-11-26 09:26:16
Original
1338 people have browsed it

Thread safety

Thread safety in java language

 1 Immutable. Immutable objects must be thread-safe

 2 For a class to be absolutely thread-safe, no matter what the runtime environment is, the caller does not need additional synchronization measures. It usually requires a large or even unrealistic price to pay. In the Java API Classes marked themselves as thread-safe, most of them are not absolutely thread-safe

 3 Relative thread-safety

 4 Thread-compatible The object itself is not thread-safe, but it can be ensured by correctly using synchronization means on the calling end. It can be used safely in a concurrent environment

 5 Thread opposition

Thread-safe implementation method

 1 Murual Exclusion & Synchronization

  Common concurrency correctness guarantee mobile phone, synchronization refers to when multiple threads concurrently access shared data. The storage certificate shared data is only used by one thread at the same time. The most basic mutual exclusion method is the synchronized keyword.

  You can also use the lock in the java.util.concurrent package to achieve synchronization ReentrantLock,

   JDK1.6 has optimized synchronized, and the efficiency is 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 lock and adaptive spin

 2. Lock elimination

 3. Lock coarsening

 4. Level lock

 5. Bias lock

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template