Java: Fourth condition of the lock: no lock, tend to lock, lock lightweight, heavyweight lock

Java Concurrency, they had really rarely used in actual projects, often we learn to forget, forget the nausea cycle at school. By learning again, have some sort of concurrent programming principles and knowledge are often confused.

synchronized

synchronized, so-called heavyweight lock. Each object in Java can be used as a lock, as follows:

  • Common methods for synchronization lock is the current instance of the object.
  • For static methods of synchronization lock is Class object of the current class.
  • A method for synchronizing blocks, the object lock is configured Synchronized brackets.

jVM synchronization and code synchronization objects to enter and exit Monitor implementation method. The method of synchronization is to use monitorenter and monitorexit instructions implemented, monitorenter instruction is inserted after compilation start position of the sync block, monitorexit is inserted at the end of the methods and exception. Synchronization method using another implementation, there is no detailed explanation in the JVM specification.

volatile

volatile is lightweight synchronized, it guarantees the shared variables in a multi-processor development visibility . Reading of a volatile variables, always able to see any thread of the last write volatile variables, having a single atomic read and write volatile variables. That is, the thread writes to volatile variables local memory will be updated to the main memory, other threads with a volatile reading of the will, must be read from the main memory of the first local invalidated.

Lock status

Where is there lock it?

Mark Work latched Java object header. Mark Work not only kept the default lock flag, also holds the object hashCode and other information. Runs, according to the lock state, modify memory contents of Mark Work . If the object is an array type, the virtual machine 3 stores word objects wide head, if the object type is not an array, the word width stored in the object 2 with the head. In the 32-bit virtual machine, a word equals four bytes wide, i.e. 32bit. First-class knowledge about the object, refer to the Java virtual machine related articles.

JVM 32 stores the default configuration of the Mark Work state.

JVM is running under 32-bit state, Mark Work storage structure.

Lock status

Lock has four states: no lock status, tend to lock, lock lightweight, heavyweight lock

With the lock of the competition, lightweight lock status from the lock to lock biased, then heavyweight lock. And the state of the lock only upgrade, not downgrade. That is only biased locking -> Lightweight lock -> Lock heavyweight, heavyweight no lock -> Lightweight lock -> biased locking.

Lockname description Scenarios
Biased locking Thread does not exist in most cases the competitive conditions, the use of synchronous consume performance, while biased locking is optimized for the lock, synchronization can be eliminated to improve performance. When a thread acquires the lock, the lock flag will object header set to be 01 to enter bias mode. Biased locking can make a thread has been holding the lock, the lock needs to compete in other threads of time, and then release the lock. Only one thread enters a critical section
Lightweight lock After obtaining biased locking thread A, thread B to enter the state competition, we need to get the thread A holds the lock, then thread A revocation biased lock into lock-free state. Thread A and thread B alternately enter the critical region, biased locking unable to meet, expansion into lightweight lock, the lock flag is set to 00. Multiple threads alternately enter the critical section
Heavyweight lock When the multi-threaded alternately enter the critical region, lightweight lock hold is maintained. However, if multiple threads simultaneously enter the critical region, hold live, and expansion to heavyweight lock Multiple threads simultaneously enter the critical section

Lock the advantages and disadvantages

lock advantage Shortcoming Applicable scene
Biased locking Locking and unlocking no additional consumption, and perform asynchronous method there is only nanosecond gap ratio If there is lock contention between threads, it will bring additional lock revocation of consumption. It applies to only one thread synchronization block access scenarios.
Lightweight lock The thread does not block competition, improve the response speed of the program. If still unable to get the spin lock contention threads consumes CPU. The pursuit of response time. Sync blocks performed very fast.
Heavyweight lock Thread competition is not using a spin, do not consume CPU Thread is blocked, slow response time The pursuit of throughput. Sync blocks long execution speed.

to sum up

In the lock state, mainly to understand what is biased lock, lock lightweight, heavyweight, and their application scenarios.

If you feel that a useful article, the article points to a praise, iron son

This article is a summary of individual learning and knowledge memos, such as incorrect or one-sided knowledge, please be corrected, thank you

Knowledge Source "Java concurrent programming art"

Guess you like

Origin blog.csdn.net/weixin_33882452/article/details/91372147