If the current thread is the holder of this lock then the hold\n * count is decremented. If the hold count is now zero then the lock\n * is released. If the current thread is not the holder of this\n * lock then {@link IllegalMonitorStateException} is thrown.\n *\n * @throws IllegalMonitorStateException if the current thread does not\n * hold this lock\n */\n public void unlock() {\n // carry 1\n sync.release(1);\n }\n\n\n public final boolean release(int arg) {\n // Successful release means true It is false\n if (tryRelease(arg)) {\n Node h = head;\n if (h != null && h.waitStatus != 0)\n // Wake up the thread behind the queue \n unparkSuccessor(h);\n // Release successful \n return true;\n }\n // Release failed \n return false;\n }\n\n\n\n protected final boolean tryRelease(int releases) {\n\n // c = 0\n int c = getState() - releases;\n // If it is not the current thread and not the exclusive thread \n if (Thread.currentThread() != getExclusiveOwnerThread())\n // Throw it wrong \n throw new IllegalMonitorStateException();\n boolean free = false;\n if (c == 0) {\n // Release the lock \n free = true;\n // Exclusive thread settings null\n setExclusiveOwnerThread(null);\n }\n // 0\n setState(c);\n return free;\n }"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The release of the lock , Relatively simple . Reset the lock state back to 0, At the same time, the exclusive thread is also set null, Then wake up the threads in the queue , Complete release ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Source code summary "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"ReentrantLock"},{"type":"text","text":" At the time of creation , Default is unfair lock , But you can also be in the construction time , You can also create a fair lock . Among them through "},{"type":"text","marks":[{"type":"strong"}],"text":"CAS"},{"type":"text","text":" change "},{"type":"text","marks":[{"type":"strong"}],"text":"state"},{"type":"text","text":" State to change the lock value , 0 Indicates that there is a lock that can be obtained ,1 Indicates that the lock has been acquired , To set the exclusive thread of the lock ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" In the fair lock mechanism , The thread that requests the lock is queued directly into a queue ( A queue simulated by a bidirectional linked list ) The last , To get the lock ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" In the mechanism of unfair lock , The thread requesting the lock will first pass through "},{"type":"text","marks":[{"type":"strong"}],"text":"CAS"},{"type":"text","text":" To change "},{"type":"text","marks":[{"type":"strong"}],"text":"state"},{"type":"text","text":" Lock state of , If you can change (0 -> 1), The lock is obtained directly , Set itself to an exclusive lock . This reduces the number of queues 、 Load queue 、 Wake up threads and other performance consumption . If it cannot be modified to "},{"type":"text","marks":[{"type":"strong"}],"text":"state"},{"type":"text","text":" The state of , It will also become a mechanism for fair lock , Enter the last of the queue , Wait for it to get the lock ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The release of the lock , take "},{"type":"text","marks":[{"type":"strong"}],"text":"state"},{"type":"text","text":" Reset back to 0, At the same time monopolize the thread ( You can also think of this as a thread object holding a lock ) Set up null, Wake up the thread that is next to it . This series of steps is done , The lock is released ."}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Advantages and disadvantages "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":" Not fair lock "},{"type":"text","text":", It's true that the performance is relatively high . But there is also an obvious disadvantage , We can imagine that , When you're in line for dinner , It's your turn to eat , At this moment, a man comes in front of you , I had a meal in advance , It has led to your rice growing , If at this time, a few people suddenly cut in front of you to make a meal , It will continue to cause you to cook longer . If you put it in the thread , Suddenly other threads get the lock in advance , That will cause the current thread to acquire the lock for a longer time , And cause the thread to block , It's too late to get the lock ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" So we have to choose the appropriate lock type according to the business , To lock , Try to avoid blocking some important business due to lock ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Statement "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" author : Sinsy Link to this article :https://blog.sincehub.cn/2020/11/10/jdk-reentrantLock/ "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Copyright notice : This article is an original blog article , follow "},{"type":"link","attrs":{"href":"https://creativecommons.org/licenses/by-sa/4.0/deed.zh","title":null},"content":[{"type":"text","text":"CC 4.0 BY-SA"}]},{"type":"text","text":" Copyright agreement , Please attach the original statement for reprint . If you have any business cooperation or authorization negotiation , Please leave me a message :[email protected]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" quote "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[1] "},{"type":"link","attrs":{"href":"https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html","title":null},"content":[{"type":"text","text":"Lock (Java Platform SE 8 )"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}