Related understanding of multi-threaded programming 2.0 - lock

Related understanding of multi-threaded programming 2.0 - lock 

For multi-threaded shared variable access security problems caused by

  • Data inconsistency problems with the expected results
    • Problem Description
      • Incrementing a program, when the operation of two threads, when the same data may be read, then it is incremented by one, and finally returns the value 1 only increased, the actual result 2 should be increased.
    • Solution
      • Increase the lock, to data security purposes and not read data being modified, allowing only read data has not been modified state.

Lock understanding

  • Java provides a locking method is Synchroinzed keyword
    • background
      • In multi-threaded programming synchronized been a veteran role, many people will call it the heavyweight lock. However, with Java SE 1.6 Dui synchronized conducted various optimization after, in some cases it is not so bad, Java SE 1.6 in order to obtain and release locks to reduce the performance overhead introduced to bring the biased locking and lightweight level locks
    • Locking way
      • Examples of modified method, the current applied to the locking instance, before entering a synchronization code to obtain the current instance of the lock
      • Static method, the lock acting on the current class objects, the synchronization code before entering the class object to obtain the current lock
      • Modifying the code block, designated lock object, to lock a given object, before entering the synchronization code library to obtain lock a given object, controlling the particle size of the different type of modification, the representative lock
  • How synchronized lock is stored
    • Observation synchronized entire grammar discovery, synchronized (lock) is based on the life cycle of this object lock to lock granularity of control
  • Object layout in memory
    • In the Hotspot virtual machine, the layout object is stored in memory can be divided into three regions: the object header (Header), instance data (Instance Data), alignment padding (the Padding)
    • Object header information
      • Generational Age
      • Lock logo
      • Whether it is biased locking
      • Timestamp, etc. tend to lock
    • Why Each object can be achieved lock
      • Each object inherits from Object, Object inside the JVM has a native C ++ object oop / oopDesc ​​the correspondence that contains information about locks, each object has a respective lock infos
      • OOP (Ordinary Object Point) means a general object pointer, Klass used to describe the specific type of the object instance. Hotspot employed instanceOopDesc ​​and arrayOopDesc ​​described object header, arrayOopDesc ​​array type is used to describe objects
    • Lock status
      • no lock
        • Lock state according to the degree of fierce competition from low to high-escalating.
      • Biased locking
        • When a thread is added a synchronization lock access code block, the current thread ID is stored in the object header, the thread enters and exits subsequent addition of this synchronization lock time code block, does not require locking and releasing the lock again. But direct comparison is stored inside the head pointing to the current thread biased locking. If equal representation biased locking is biased in favor of the current thread, do not need to try to get a lock, a process referred to as CAS .
      • Lightweight lock
        • Lightweight locked in the locking process, used the so-called spin-spin locks, refers to the time when there is another thread to compete lock this thread will wait for the cycle in place, rather than the thread to block until the get after the thread releases the lock of the lock, the thread can acquire the lock immediately.
        • Note that lock in place when the cycle is to consume the cpu, equivalent to some for a loop in execution had nothing. So, lightweight lock applies to those scenes synchronized block quickly executed, so that the thread still waiting for a very short time to be able to get a lock. The use of spin locks, there is also a certain probability that background, most of the time in the synchronized block execution is very short. So seemingly without objection by the cycle but can enhance the performance of the lock. However, certain conditions must be spin control, or if a thread executing synchronized block of time is very long, then the thread continues to cycle but will consume CPU resources. The number of spins is 10 by default, can be modified by preBlockSpin
        • After JDK1.6, the introduction of adaptive spin lock, adaptive means that the number of spin is not fixed, but according to state once in the same spin lock before time, and the lock owner decision.
        • MarkWord lightweight lock lock release logic is actually the reverse logic to acquire a lock, the CAS operation by the thread stack frame LockRecord back to replace the lock object, if successful, that there is no competition. If it fails, there is competition represents the current lock, then the lock will be expanded into a lightweight heavyweight lock
      • Heavyweight lock
        • When the lightweight heavyweight lock to lock inflation, can only mean that the thread is suspended waiting to be blocked to wake up. Monitor will monitor synchronization queue heavyweight locks, execute a new thread after thread lock out queue.
    • CASE
      • The acronym compare and swap, that is what we call comparative exchange. cas is a lock-based operation, and is optimistic locking. In java lock into optimistic and pessimistic locking. Pessimistic lock is locked resource, such as after a thread releases the lock before obtaining the lock, the next thread can access. The optimistic locking taking a broad approach to processing resources by not locking in some way, such as to obtain the data by adding to the record version, a more pessimistic locking performance has greatly improved.
    • supplement
      • In our application development, certainly there will be more than two threads compete in most cases, if you tend to open the lock, it will enhance access to resources depletion lock. Therefore, the parameters can be set by jvm UseBiasedLocking biased locking on or off
    • Lock state chart
    •  
    • Biased locking is obtained flowchart revocation
    • Lightweight locks expansion flowchart obtained

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/erfsfj-dbc/p/11918116.html