JMM

Java Memory Model

Java memory model

Concurrent programming

Volatile

The assembly code generated by Volatile variables is prefixed with a Lock instruction that acts as a memory barrier.

  • The order of instruction reordering is ensured
  • Forcing cache changes to be immediately changed to main memory is equivalent to directly performing operations on main memory

Volatile is used primarily when multiple threads perceive that an instance variable has changed, allowing each thread to obtain the latest value.

Why Volatile

Atomic classes

CAS

invariance

Java lock

Mutual exclusion and synchronization

Mutexes cannot run at the same time. If one thread acquires the lock, other threads will block

A number of program segments that are interspersed between different processes. When one process runs one of the segments, other processes cannot run any of the segments until the process has finished running the segment.

The so-called synchronization, – cannot run at the same time on the basis of a certain order to run

A number of pieces of program that move between different processes and must be executed in strict accordance with a defined sequence that depends on the specific task to be accomplished.

Synchronization is a more complex kind of mutex, and mutex is a special kind of synchronization.

Non-mutex synchronization – the thread does not block and can loop to try

The classification of the lock

Optimistic lock — Pessimistic lock

  • Pessimistic locks: — Synchronized Lock interface

    The advantage is simple and safe, but the disadvantage is that because both the suspended thread and the resumed thread need to go into the kernel state, in the real situation, the probability of conflict is very small, pessimistic lock will cause waste

  • Optimistic locking: — Generally implemented through CAS, such as atomic classes, concurrent containers, etc

    If the data is different from the original one, it indicates that the data has been modified during this period of time. Therefore, the system cannot continue to update the data, and will abort, report an error, or retry. Such as adding a version number to the database

Fair lock – Unfair lock

Read-write lock

Reentrant lock — Non-reentrant lock

Spin lock – Adaptive lock

Interruptible lock

Bias lock, lightweight lock, heavyweight lock

These three locks refer to lock states and are aimed at Synchronized. In Java, lock upgrade mechanism is introduced to achieve efficient Synchronized

Synchronized

  • Object lock

  • Kind of lock

The principle of

Java object head

Synchronized optimization

  • Unlocked state
  • Biased locking
  • Lightweight lock
  • Heavyweight lock

Biased locking

Lightweight lock

Heavyweight lock

Lock coarsening

Lock elimination

Reentrant lock

It is permitted for a thread to call a Synchronized method while calling another Synchronized method inside its method body, which means that a thread has acquired an object lock and then requests it again.

Do not interrupt

  • Interrupt threads

Wake-on-wait mechanism and Synchronized

Producer-consumer model

ReentrantLock

Concurrent container utility classes

Concurrent container

ConcurrnetHashMap

  • Java7

  • Java8

  • others

CopyOnWriteArrayList/Set

  • The iteration

Concurrent flow control

CountDownLatch

Semaphore

Semaphore license

CycicBarrier

reusable

AQS

Abstract Queued Synchronized

Multithreaded debugging

High-performance cache instance