Hello [interviewer] 011 Java Concurrency underlying principle of the lock Detailed

Micro-channel public number: Hello interviewer
no fragmentation of knowledge here, only a complete system of knowledge.
Focus on comprehensive knowledge points to explain the system, interview subject resolve;
if you feel article helpful to you, welcome attention, share appreciation .

### Foreword
two eggs a few days did not receive an interview, that he has been cool, I did not expect at this time once again received an invitation to interview, so in a sunny afternoon, about two eggs such as sitting across from the interviewer, I began the interview today.

Interviewer : Young man, we do not continue introduced, you have come several times, this time let's come to the point of it.

###text

Tell us what is the lock?

Lock is used to control a plurality of threads shared resource access mode, in general, a plurality of threads can be prevented from lock access a shared resource at the same time (but may allow for some locks concurrently by multiple threads to access shared resources, such as read-write lock). Before Lock interface goes, Java program is realized by the synchronized keyword lock function, and after Java SE 5, and added a contracting Lock Interface (and the associated implementation class) to achieve lock feature, which provides critical and synchronized similar synchronization word, except when in use need to explicitly acquiring and releasing locks. Although it lacks the implicit obtain the release of the lock (through synchronized blocks or methods provided) convenience, but it has a variety of synchronized lock acquisition and release key operability, and interruptible lock acquisition timeouts acquire lock do not have the word synchronization characteristics.

Using the synchronized keyword will implicitly acquire a lock, but it will lock acquisition and release of cure, and that is to get re-released. Of course, this approach simplifies the synchronization of management, but does not show the extensible lock acquire and release to the good. For example, for a scene, hands for lock acquisition and release, to get the lock A, and then acquire the lock B, when the lock B is obtained, release the lock A simultaneously acquire lock C, when the lock C is obtained, then release the B simultaneously acquire lock D, and so on. In this scenario, synchronized keyword is not so easy to implement, while using Lock it much easier.

Lock is an interface that defines the basic operation of the lock acquisition and release.

The underlying principle of the lock is how to achieve?

Implementation mechanism locks depends on the queue synchronizer, which uses an int member variable indicates the synchronization status, access to resources to complete the work queue threads through built-in FIFO queue, able to achieve most of the synchronization requirements.

The synchronizer comprises two types of application nodes, a first node point, a node pointing to the tail, the thread lock is not acquired node creates thread safe (compareAndSetTail) was added end of the queue. Synchronous queue follow FIFO, the first node is to obtain a successful synchronization status node.

Did not get to lock threads will create a node, set to the end node. As shown below:

thread head node when the lock is released, it will wake up the subsequent node. The successor node will be successful in acquiring a lock set itself a head node. As shown below:

Exclusive lock acquisition and responsive

  • Exclusive: There is one and only one thread can acquire the lock.
  • Shared: multiple threads can simultaneously acquire the lock.

Exclusive: Each node spin observe their own previous node is not the Header node, if it is, go try to get a lock.

Exclusive lock acquisition process:

Shared and exclusive difference:

Share lock acquisition process:

Interviewer : ah, today's interview right here, go back and prepare for the next interview now.



There is no fragmentation of knowledge, only a complete system of knowledge.
Focus on comprehensive knowledge points to explain the system, interview subject resolve;
if you feel article helpful to you, welcome attention, share appreciation .

发布了97 篇原创文章 · 获赞 47 · 访问量 14万+

Guess you like

Origin blog.csdn.net/Momentyol/article/details/104520159