Talk about lock

Lock fair / unfair Lock

  1. It refers to a fair lock request in order to lock the dispensing lock.
  2. Unfair course, is not by the order of allocation lock application lock, of course, which may cause hunger.
  3. ReentrantLock can specify whether the lock is constructed by fair locks, which is achieved internal thread scheduling by AQS, it is possible to become fair locks. The default is non-fair locks, which will be able to increase its throughput.
  4. non-synchronized lock fair, because unlike Reentrantlock AQS can be implemented thread scheduling and so it can not be an equitable lock.

Reentrant lock

  1. As the name suggests, when one thread acquires a lock, the lock can be obtained again.
  2. Thread and shop and go to other things or pause off, the lock will not be released
  3. synchronized is reentrant lock in Java is an important realization.
  4. Said lower reentrant lock how to avoid deadlock, look at an example
//在同一对象中有
synchronized void outerEnter() throws Exception{
    innerEnter();
}
synchronized void innerEnter() throws Exception{
  //do something
}
复制代码
  • Resolution:
    1. In the above code, we first call outerEnter () method to obtain monitoring lock for this object (here do not understand this lock can jump into the portal )
    2. In this method we went to another call is synchronized modified innerEnter () method, there will once again go to pick up the monitor lock of the object注意:这里是同一把锁
    3. This assumes a situation, if there is no re-entry lock this feature, when expressed as mutually exclusive, when I was in the case of acquiring the lock again to get the same lock, I need to wait for the release of the lock, but I own while holding the lock, so I'll wait until I look forward to the lock, thereby forming a deadlock
    4. In fact, here is the destruction of one of the mutually exclusive conditions for the formation of a deadlock condition and could well avoid deadlock

Exclusive and shared locks

  1. Exclusive lock means that at the same time, a lock can only be owned by a single thread lock, reflecting mutually exclusive. Conversely, a shared lock is to be owned by more than one thread lock.
  2. Mysql to get read-write lock Example: When data is read using a shared lock, other transactions need to read the time to add a shared lock can achieve read. 3. Data on the line as long as there is a read lock it shows there are other matters in reading, can not be changed, it can not add to it a write lock - exclusive lock.

Optimistic and pessimistic locking

  1. Call it optimistic locking is that when we make changes to the data, it is optimistic that the data we will read it down -> Edit out -> Back into this stage of the operation will not be modified other concurrent, so no need to add more data to give this a mutex.
  2. On the contrary, pessimistic locking think you to modify a data, it will be a file modified by other concurrent operations, so I have to add to it a lock to ensure the accuracy of the data.
  3. Java in the CAS (compare and swap) belongs to an optimistic locking, that is, without a specific lock, while the pessimistic lock is a lock to add data, no matter what the lock is locked.
  4. They say that the scenarios it: optimistic locking for very many read the situation, because I was reading when avoiding the lock, the lock is released, greatly improving performance. Pessimistic lock is applied to write a lot of cases.
  5. Take mysql table to maintain the optimistic lock to, for example, in a mysql table, there must be a field ObjectVersionNumber, in fact, this is an optimistic locking. When taking data, we do not control it wants to be read directly. When modifying data, modify data once we put ObjectVersionNumber every field plus 1, if there are two transactions at the same time a data operation, and that a transaction commits, changes are complete, another submission, I found my last taken out of ObjectVersionNumber and data inside the database is now inconsistent, indicating someone to turn over, so this time I executed the transaction will fail. Thus ensuring the accuracy of the data.

Segmented lock

  1. Refers to a segment lock set of data, it is divided a plurality of times, each of the divided out of a separate lock. But when the data is required to operate, this one currently accessed individually lock, other transactions accessing other blocks will not be blocked off, and accordingly will be able to improve operational efficiency.
  2. ConcurrentHashMap is segmented lock in Java important to realize - later I'll write the content ConcurrentHashMap stay tuned.

Finally, talk about upgrades and downgrades Java1.5 after lock

  1. Which is mainly related to the Synchronized for three types of lock status - biased lock, lock lightweight, heavyweight lock.
  2. Lock state is indicated by the monitor object is in the object field of the header.
  3. First biased locking, it refers to the period of synchronization code has been accessed by a thread, no other concurrent operations, then the thread will automatically obtain the lock. Reduce the cost to acquire a lock.
  4. Lightweight lock means that when the lock is biased locking time, be accessed by another thread, there have been competing lock, the lock will be upgraded to lightweight iu lock, another thread tries to acquire a lock in the form of spin, without blocking and improve performance. Spin refers here to obtain the lock thread does not block immediately, instead of using a circular fashion to try to acquire the lock, this benefit is to reduce the consumption of thread context switching, no need to go to the heavyweight lock.
  5. Heavyweight lock means when the lock is a lightweight lock when another thread though is the spin, but spin will not last forever, when the spin a certain number of times, has not yet acquired lock, will enter obstruction the lock inflation heavyweight lock. Heavyweight thread lock to make other applications into the blocking performance.

Reference from Synchronized with the difference between the scenarios Lock

Guess you like

Origin juejin.im/post/5d6a0676f265da03bd05331f