The interviewer let me talk about Java in the lock, I laughed

Reprinted from   the interviewer let me talk about Java in the lock, I laughed

In many concurrent reading the article will mention a wide variety of locks such as fair locks, optimistic locks, and so on, this article describes the classification of various locks. Introduction of the following:

  • Lock fair / unfair Lock

  • Reentrant lock

  • Exclusive lock / shared lock

  • Mutex / read-write lock

  • Lock optimistic / pessimistic locking

  • Segmented lock

  • Biased locking / lock lightweight / heavyweight lock

  • Spinlocks

     

The above is a lot of locks term, these categories are not all referring to the lock state, some refer to characteristics of the lock, the lock refers to some of the design, content is summarized below for each lock a certain interpretation of the term.

Lock fair / unfair Lock

Fair lock means that multiple threads in order to apply the lock to get the lock. Unfair lock refers to the order of multiple threads to obtain the lock is not in order to apply the lock, it is possible to apply the thread after thread priority to acquire the lock than prior application. Possible cause priority inversion or hunger. For Java ReentrantLock, the constructor specify whether the lock by lock is fair, non-default fair locks. Unfair advantage that the lock is larger than the throughput fairness lock. For Synchronized, it is also an unfair lock. Because it is not as ReentrantLock thread scheduling is achieved by AQS (AbstractQueuedSynchronizer), so and there is no way that it becomes fair locks.

Reentrant lock

Reentrant lock known recursive lock means when the same thread acquires the lock of the outer layer method, the method will automatically get into the inner lock. Said a little abstract, there will be following the example of a code. For Java ReentrantLock concerned, his name can be seen be a reentrant lock, whose name is Re entrant Lock re-enter the lock. For Synchronized, also a reentrant lock. One benefit of reentrant lock is to some extent avoid deadlocks.

synchronized void setA() throws Exception{

   Thread.sleep(1000);

   setB();

}


synchronized void setB() throws Exception{

   Thread.sleep(1000);

}

The code above is a re-lock into a characteristic, if not reentrant lock, then, setB may not be current thread of execution, may cause a deadlock.

Exclusive lock / shared lock

  • Exclusive lock means the lock can only be held by a thread.

  • Shared lock means the lock can be held by multiple threads.

For Java ReentrantLock concerned, it is the exclusive lock. But for another class that implements ReadWriteLock Lock, which read lock is a shared lock, which locks are exclusive write locks. Shared lock to read lock ensures very efficient concurrent read, write, write, read, write processes are mutually exclusive. Exclusive lock and a shared lock is achieved through the AQS, by implementing different methods to achieve exclusive or shared. For Synchronized, of course, is the exclusive lock.

Mutex / read-write lock

Mentioned above exclusive lock / shared lock is a generalized statement, mutex / write lock is a specific implementation.

  • Mutex specific implementation in Java is ReentrantLock

  • Specific read and write lock in Java is ReadWriteLock

Lock optimistic / pessimistic locking

Optimistic and pessimistic locking lock does not refer specifically what types of locks, but to look at the perspective of concurrent synchronization.

  • Pessimistic locking think the same data for concurrent operation, it must be modified will happen, even if not modified, will be considered to be modified. So for the same data concurrency, pessimistic locking in the form of lock. Pessimistic view that unlocked concurrent operation will go wrong.

  • Optimistic locking is considered for the same data concurrency is not going to happen modified. When updating data, will be used to try to update constantly re-update the data. Optimistic that unlocked concurrent operation is no matter.

We can see from the above description, pessimistic locking for write a lot of scenarios, optimistic locking for read a lot of the scene, unlocked will bring a lot of performance improvements. Pessimistic locking in Java use, is to use a variety of locks. Optimistic locking in Java, no lock is programmed, CAS algorithm is often used, a typical example is based atoms, atomic update operation to achieve spin by CAS.

Segmented lock

A lock is actually the lock segment design, not a specific lock, for ConcurrentHashMap, its implementation is complicated to achieve efficient concurrent operation of the lock in the form of segments.

We ConcurrentHashMap speaking about the meaning and segmented lock design, ConcurrentHashMap the lock segment called Segment, its structure that is similar to HashMap (JDK7 and JDK8 in HashMap implementation), that has an internal Entry array, the array each element is a linked list; but it is also a ReentrantLock (Segment inherited ReentrantLock).

When you need to put the elements, not the entire hashmap be locked, but the first to know that he wants to put a segment, then the segments were locked by hashcode, so when multiple threads put the time, As long as not on a segment, to achieve a true parallel insertion.

However, statistics size at the time, but I could get hashmap global information when you need to get all of the segments to locks statistics.

Is designed to lock segment refinement lock granularity, the entire array need not be updated when the operation time, it performs a locking operation with respect to only one array.

Biased locking / lock lightweight / heavyweight lock

The three lock refers to a lock state, and is for Synchronized. In Java 5 to achieve efficient Synchronized mechanism by introducing lock escalation. Three lock state is established by the object in the object field of the monitor to indicate the head.

  • Biased locking means for a synchronization code has been accessed by a thread, then the thread will automatically obtain the lock. Reduce the cost to acquire a lock.

  • Lightweight lock means that when the lock is biased locking time, be accessed by another thread, it will be upgraded to lightweight biased locking lock, another thread tries to acquire a lock in the form of spin, does not block, improve performance .

  • 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.

Spinlocks

In Java, the spin lock refers to the attempt to acquire 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, the drawback is that the cycle will consume CPU. Typical examples spinlock implemented, reference may be implemented spin lock

发布了354 篇原创文章 · 获赞 522 · 访问量 128万+

Guess you like

Origin blog.csdn.net/moakun/article/details/104033382