[Reprint] biased locking threads and lightweight lock, organize knowledge and biased locking thread, knowledge lightweight locks, lock finishing heavyweight heavyweight lock

Biased locking thread and knowledge lightweight locks, lock finishing heavyweight

 
https://www.cnblogs.com/xlecho/p/11342971.html

 

xl_echo edited, welcome to reprint, please declare the source of the article. Welcome to add echo micro letter (Micro Signal: t2421499075) the exchange of learning. Battle undefeated, according to not claiming victorious, defeat after defeat is not decadent, according to energy struggling to move forward. - This is the real rated power! !


Reference list of articles:

Java Concurrency: Synchronized underlying optimization (biased locking, lightweight lock)

Lightweight lock, tend to lock, lock heavyweight details

Biased lock, lock lightweight, heavyweight lock, spin-lock principle to explain (look at recommended)

Reference Video: college foam cushions Mic teacher of the basic principles of multi-threaded


The main contents are as follows

  • Security issues simultaneously multi-threaded execution of thinking
  • Synchronized basic understanding of
  • Thinking lock storage
  • Upgrade principle Synchronized lock
  • wait / notify implement thread communication

Security issues simultaneously multi-threaded execution of thinking

If the business logic of the code which has a changing operation requires a constant value, such as int i = 0, i need service code which i ++. In the case of single-threaded, no problem, if it is multi-threaded operating value i, this time, what is the final result i? Security thread will appear. This situation should be how to solve?

There are three security thread

  • Atomicity
    • Provides exclusive access, only one thread at a time to operate it
    • Two ways to achieve lock:
      . 1) the synchronized: in the scope of action of the object, JVM implementation dependent atomic operation.
      2) Lock: dependent special CPU instructions, code.
  • Visibility
    • A thread changes to the main memory can be observed in other threads timely.
    • The specific implementation process:
      1) when the variable write operation to the volatile, the barrier will add a store instruction of the write operation, the local memory shared variable values flushed to main memory.
      2) When the variable volatile read operation, a load will be added to the barrier instruction before a read operation, a read of the shared variable from main memory.
  • Orderliness
    • Java memory model, allowing the compiler and processor instructions to be reordered, but the reordering process will not affect the implementation of single-threaded programs, but it will affect the accuracy of concurrent execution of multiple threads.

Synchronized basic understanding of

Synchronized is a synchronous lock in java which is also a mutex, when there is a thread to get the lock when the other threads to get. In jdk1.6 previous Synchronized is the weight of the lock, then do the relevant optimization, performance has some improvement.

  • The basic use of Synchronized
    • Examples of modification methods
    • Modified static method
    • Modified block

Thinking lock storage

Synchronized locking storage

  • Object layout in memory
  • Mark Word

There are four lock status

  • Lock-free status
  • Biased locking
  • Lightweight lock
  • Heavyweight lock

    There are four state of the lock: no lock status, tend to lock, lightweight and heavyweight lock lock. With the lock of the competition, you can upgrade from lock to lock biased lightweight lock, then upgrade heavyweight lock (lock but the upgrade is unidirectional, that is to say only upgrade from low to high, the lock will not appear downgrade). JDK 1.6 is enabled by default in biased locking lock and lightweight, we can also -XX: Disable biased locking -UseBiasedLocking. The lock state is stored in the file header object to the JDK Example 32:

Lock status 25bit 4bit 1bit 2bit
23bit 2bit Whether or not biased locking (whether disabled bias) Lock flag
No lock state hashCode object Generational Age 0 01
Lightweight lock Pointing the stack pointer record lock 00
Heavyweight lock Mutex pointer (heavyweight lock) of 10
GC mark air 11
Biased locking Thread ID Epoch Generational Age 1 01

These locks are not equivalent to the Java API ReentratLock such locks, these locks are conceptual, the lock mechanism JDK1.6 in order to synchronize synchronized keyword optimization generated. These locks startup and shutdown policies can be set by setting the JVM startup parameters, of course, in general, use the default JVM strategy on it.

Biased locking

Popular speaking, is biased locking during operation, the lock object toward a thread. That is, in the case of open biased locking mechanism, a thread to acquire a lock, when the thread next time you want to get a lock, do not need to acquire a lock (ie ignoring synchronized keyword), you can directly execute synchronization code, more suitable competition less.

Lightweight lock

Lightweight locks are not intended to replace the traditional heavyweight lock, but in the absence of competition multithreading using lightweight lock consumption performance can be reduced, but when a plurality of threads simultaneously lock contention, lightweight lock It expands heavyweight lock.

Heavyweight lock

That is, when there are other threads holding the lock, the current thread into the blocked state.

wait / notify implement thread communication

wait: release the lock, blocking the thread
notify: wake up a thread

xl_echo edited, welcome to reprint, please declare the source of the article. Welcome to add echo micro letter (Micro Signal: t2421499075) the exchange of learning. Battle undefeated, according to not claiming victorious, defeat after defeat is not decadent, according to energy struggling to move forward. - This is the real rated power! !


Reference list of articles:

Java Concurrency: Synchronized underlying optimization (biased locking, lightweight lock)

Lightweight lock, tend to lock, lock heavyweight details

Biased lock, lock lightweight, heavyweight lock, spin-lock principle to explain (look at recommended)

Reference Video: college foam cushions Mic teacher of the basic principles of multi-threaded


The main contents are as follows

  • Security issues simultaneously multi-threaded execution of thinking
  • Synchronized basic understanding of
  • Thinking lock storage
  • Upgrade principle Synchronized lock
  • wait / notify implement thread communication

Security issues simultaneously multi-threaded execution of thinking

If the business logic of the code which has a changing operation requires a constant value, such as int i = 0, i need service code which i ++. In the case of single-threaded, no problem, if it is multi-threaded operating value i, this time, what is the final result i? Security thread will appear. This situation should be how to solve?

There are three security thread

  • Atomicity
    • Provides exclusive access, only one thread at a time to operate it
    • Two ways to achieve lock:
      . 1) the synchronized: in the scope of action of the object, JVM implementation dependent atomic operation.
      2) Lock: dependent special CPU instructions, code.
  • Visibility
    • A thread changes to the main memory can be observed in other threads timely.
    • The specific implementation process:
      1) when the variable write operation to the volatile, the barrier will add a store instruction of the write operation, the local memory shared variable values flushed to main memory.
      2) When the variable volatile read operation, a load will be added to the barrier instruction before a read operation, a read of the shared variable from main memory.
  • Orderliness
    • Java memory model, allowing the compiler and processor instructions to be reordered, but the reordering process will not affect the implementation of single-threaded programs, but it will affect the accuracy of concurrent execution of multiple threads.

Synchronized basic understanding of

Synchronized is a synchronous lock in java which is also a mutex, when there is a thread to get the lock when the other threads to get. In jdk1.6 previous Synchronized is the weight of the lock, then do the relevant optimization, performance has some improvement.

  • The basic use of Synchronized
    • Examples of modification methods
    • Modified static method
    • Modified block

Thinking lock storage

Synchronized locking storage

  • Object layout in memory
  • Mark Word

There are four lock status

  • Lock-free status
  • Biased locking
  • Lightweight lock
  • Heavyweight lock

    There are four state of the lock: no lock status, tend to lock, lightweight and heavyweight lock lock. With the lock of the competition, you can upgrade from lock to lock biased lightweight lock, then upgrade heavyweight lock (lock but the upgrade is unidirectional, that is to say only upgrade from low to high, the lock will not appear downgrade). JDK 1.6 is enabled by default in biased locking lock and lightweight, we can also -XX: Disable biased locking -UseBiasedLocking. The lock state is stored in the file header object to the JDK Example 32:

Lock status 25bit 4bit 1bit 2bit
23bit 2bit Whether or not biased locking (whether disabled bias) Lock flag
No lock state hashCode object Generational Age 0 01
Lightweight lock Pointing the stack pointer record lock 00
Heavyweight lock Mutex pointer (heavyweight lock) of 10
GC mark air 11
Biased locking Thread ID Epoch Generational Age 1 01

These locks are not equivalent to the Java API ReentratLock such locks, these locks are conceptual, the lock mechanism JDK1.6 in order to synchronize synchronized keyword optimization generated. These locks startup and shutdown policies can be set by setting the JVM startup parameters, of course, in general, use the default JVM strategy on it.

Biased locking

Popular speaking, is biased locking during operation, the lock object toward a thread. That is, in the case of open biased locking mechanism, a thread to acquire a lock, when the thread next time you want to get a lock, do not need to acquire a lock (ie ignoring synchronized keyword), you can directly execute synchronization code, more suitable competition less.

Lightweight lock

Lightweight locks are not intended to replace the traditional heavyweight lock, but in the absence of competition multithreading using lightweight lock consumption performance can be reduced, but when a plurality of threads simultaneously lock contention, lightweight lock It expands heavyweight lock.

Heavyweight lock

That is, when there are other threads holding the lock, the current thread into the blocked state.

wait / notify implement thread communication

wait: release the lock, blocking the thread
notify: wake up a thread

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11842648.html