{"id":618,"date":"2013-12-09T15:49:39","date_gmt":"2013-12-09T07:49:39","guid":{"rendered":"http:\/\/coderbee.net\/?p=618"},"modified":"2014-02-21T22:37:12","modified_gmt":"2014-02-21T14:37:12","slug":"juc-%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90-%e4%ba%8c-reentrantlock","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20131209\/618","title":{"rendered":"JUC \u6e90\u7801\u5206\u6790 \u4e8c ReentrantLock"},"content":{"rendered":"<p><a href=\"http:\/\/coderbee.net\/index.php\/concurrent\/20131209\/614\">\u4e0a\u4e00\u7bc7\u6587\u7ae0<\/a>\u5df2\u5b8c\u6574\u5c55\u793a\u4e86\u7b49\u5f85\u961f\u5217\u7684\u7ba1\u7406\uff08\u6dfb\u52a0\u7ed3\u70b9\u3001\u79fb\u9664\u53d6\u6d88\u7ed3\u70b9\uff09\u3001\u72ec\u5360\u6a21\u5f0f\u4e0b\u7684acquire\u64cd\u4f5c\u3001acquire\u4e2d\u65ad\u53d6\u6d88\u3001\u524d\u9a71\u5982\u4f55\u901a\u77e5\u540e\u7ee7\u3002\u8fd9\u4e9b\u77e5\u8bc6\u5df2\u8db3\u591f\u7528\u6765\u5b9e\u73b0\u4e00\u4e2a\u53ef\u91cd\u5165\u9501\u3002<\/p>\n<p>\u672c\u7bc7\u901a\u8fc7<code>java.util.concurrent.locks.ReentrantLock<\/code>\u7c7b\u7684\u6e90\u7801\u6765\u5206\u6790\u5982\u4f55\u5b9e\u73b0\u53ef\u91cd\u5165\u9501\u3002<\/p>\n<h3>\u53ef\u91cd\u5165\u9501<\/h3>\n<p>\u53ef\u91cd\u5165\u9501\u5c31\u662f\u8bf4\u5f53\u7ebf\u7a0b\u62e5\u6709\u8fd9\u628a\u9501\u7684\u65f6\u5019\uff0c\u5b83\u518d\u6b21\u8bf7\u6c42\u9501\u662f\u6210\u529f\u7684\uff1b\u5f53\u7ebf\u7a0b\u91ca\u653e\u9501\u65f6\uff0c\u5982\u679c\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u5bf9\u9501\u7684\u8bf7\u6c42\u6b21\u6570\u5927\u4e8e\u91ca\u653e\u6b21\u6570\uff0c\u5219\u8be5\u7ebf\u7a0b\u4ecd\u7136\u62e5\u6709\u9501\uff0c\u76f4\u5230\u8bf7\u6c42\u6b21\u6570\u4e0e\u91ca\u653e\u6b21\u6570\u76f8\u7b49\u65f6\u624d\u771f\u6b63\u91ca\u653e\u9501\u3002<\/p>\n<p>\u6240\u4ee5\u53ef\u91cd\u5165\u9501\u9700\u8981\u4e00\u4e2a\u91cd\u5165\u8ba1\u6570\u53d8\u91cf\uff0c\u521d\u59cb\u503c\u8bbe\u4e3a0\uff0c\u5f53\u6210\u529f\u8bf7\u6c42\u9501\u65f6\u52a01\uff0c\u91ca\u653e\u9501\u65f6\u51cf1\uff0c\u5f53\u91ca\u653e\u9501\u4e4b\u540e\u8ba1\u6570\u4e3a0\u5219\u771f\u6b63\u91ca\u653e\u9501\u3002\u91cd\u5165\u9501\u8fd8\u5fc5\u987b\u6301\u6709\u5bf9\u9501\u6301\u6709\u8005\u7684\u5f15\u7528\uff0c\u7528\u4ee5\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u91cd\u5165\u3002<\/p>\n<h3>\u9501\u7684\u516c\u5e73\u6027<\/h3>\n<p>\u5982\u679c\u9501\u80fd\u591f\u4e25\u683c\u6309\u7167\u7ebf\u7a0b\u8bf7\u6c42\u9501\u7684\u5148\u540e\u987a\u5e8f\u5206\u914d\u9501\uff0c\u5219\u8ba4\u4e3a\u9501\u5177\u6709\u516c\u5e73\u6027\uff1b\u5982\u679c\u67d0\u4e00\u7ebf\u7a0b\u80fd\u5728\u5176\u4ed6\u7b49\u5f85\u7ebf\u7a0b\u4e4b\u524d\u83b7\u53d6\u5230\u9501\uff0c\u5219\u8ba4\u4e3a\u9501\u4e0d\u5177\u6709\u516c\u5e73\u6027\u3002<\/p>\n<h3>ReentrantLock<\/h3>\n<p><code>ReentrantLock<\/code>\u662fJUC\u5305\u91cc\u53ef\u91cd\u5165\u7684\u72ec\u5360\u9501\u5b9e\u73b0\uff0c\u5b83\u5177\u6709\u4e09\u4e2a\u5185\u90e8\u7c7b\uff1a<code>Sync\u3001NonfairSync\u3001FairSync<\/code>\uff0c\u901a\u8fc7\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u6765\u6307\u5b9a\u9501\u662f\u5426\u662f\u516c\u5e73\u7684\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u6838\u5fc3\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">public class ReentrantLock implements Lock, java.io.Serializable {\n     private final Sync sync;\n\n     public ReentrantLock(boolean fair) {\n         sync = fair ? new FairSync() : new NonfairSync();\n     }\n\n     public void lock() {\n         sync.lock();\n     }\n\n     public void unlock() {\n         sync.release(1);     \/\/ \u8fd9\u4e2a1\u8868\u793a\u9000\u51fa\u95011\u6b21\u3002\n     }\n\n     \/\/ \u5e26\u8d85\u65f6\u9650\u5236\u7684\u83b7\u53d6\n  public boolean tryLock(long timeout, TimeUnit unit)\n          throws InterruptedException {\n      return sync.tryAcquireNanos(1, unit.toNanos(timeout));\n  }\n\n     \/\/ \u5176\u4ed6\u4ee3\u7801\u7701\u7565\n}\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>ReentrantLock<\/code>\u90fd\u662f\u628a\u5177\u4f53\u5b9e\u73b0\u59d4\u6258\u7ed9\u5185\u90e8\u7c7b\u800c\u4e0d\u662f\u76f4\u63a5\u7ee7\u627f\u81ea<code>AbstractQueuedSynchronizer<\/code>\uff0c\u8fd9\u6837\u7684\u597d\u5904\u662f\u7528\u6237\u4e0d\u4f1a\u770b\u5230\u4e0d\u9700\u8981\u7684\u65b9\u6cd5\uff0c\u4e5f\u907f\u514d\u4e86\u7528\u6237\u9519\u8bef\u5730\u4f7f\u7528<code>AbstractQueuedSynchronizer<\/code>\u7684\u516c\u5f00\u65b9\u6cd5\u800c\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p><code>ReentrantLock<\/code>\u7684\u91cd\u5165\u8ba1\u6570\u662f\u4f7f\u7528<code>AbstractQueuedSynchronizer<\/code>\u7684<code>state<\/code>\u5c5e\u6027\u7684\uff0c<code>state<\/code>\u5927\u4e8e0\u8868\u793a\u9501\u88ab\u5360\u7528\u3001\u7b49\u4e8e0\u8868\u793a\u7a7a\u95f2\uff0c\u5c0f\u4e8e0\u5219\u662f\u91cd\u5165\u6b21\u6570\u592a\u591a\u5bfc\u81f4\u6ea2\u51fa\u4e86\u3002<br \/>\n<!--more--><\/p>\n<h3>ReentrantLock.Sync<\/h3>\n<p>\u53ef\u91cd\u5165\u9501\u5185\u90e8\u5b9e\u73b0\u7684\u8d85\u7c7b\uff0c\u4e3b\u8981\u5b9e\u73b0\u4e86\u516c\u5e73\u4e0e\u975e\u516c\u5e73\u9501\u7684\u5171\u6709\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e86\u52a0\u9501\u64cd\u4f5c\u7684\u7edf\u4e00\u62bd\u8c61\uff1a<code>abstract void lock();<\/code>\uff0c\u8fd8\u6709\u6838\u5fc3\u7684\u91ca\u653e\u9501\u7684\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"java\">abstract static class Sync extends AbstractQueuedSynchronizer {\n    private static final long serialVersionUID = -5179523762034025860L;\n\n    \/\/ \u6267\u884cLock.lock()\u65b9\u6cd5\uff0c\u7559\u7ed9\u5b50\u7c7b\u6839\u636e\u5176\u516c\u5e73\u6027\u5b9e\u73b0\u3002\n    \/\/ \u5b50\u7c7b\u5316\u7684\u6700\u4e3b\u8981\u539f\u56e0\u662f\u5141\u8bb8\u975e\u516c\u5e73\u7684\u5feb\u901f\u8def\u5f84\u3002\n    abstract void lock();\n\n    \/\/ \u975e\u516c\u5e73\u83b7\u53d6\n    final boolean nonfairTryAcquire(int acquires) {\n        final Thread current = Thread.currentThread();\n        int c = getState();\n        if (c == 0) {\n      \/\/ \u9501\u662f\u7a7a\u95f2\u7684\uff0c\u8fdb\u884c\u52a0\u9501\u5fc5\u987b\u7528CAS\u6765\u786e\u4fdd\u5373\u4f7f\u6709\u591a\u4e2a\u7ebf\u7a0b\u7ade\u4e89\u9501\u4e5f\u662f\u5b89\u5168\u7684\n            if (compareAndSetState(0, acquires)) {\n                \/\/ \u52a0\u9501\u6210\u529f\n\n                \/\/ \u628a\u5f53\u524d\u7ebf\u7a0b\u8bbe\u4e3a\u9501\u7684\u6301\u6709\u8005\uff0c\u5728\u83b7\u53d6\u524d\u53ef\u7528\u4e8e\u5224\u65ad\u662f\u5426\u662f\u91cd\u5165\u3002\n                setExclusiveOwnerThread(current);\n                return true ;\n            }\n        }\n        else if (current == getExclusiveOwnerThread()) {\n             \/\/ \u9501\u88ab\u5360\u7528\u4e14\u5f53\u524d\u7ebf\u7a0b\u662f\u9501\u7684\u6301\u6709\u8005\uff0c\u8bf4\u660e\u662f\u91cd\u5165\u3002\n            int nextc = c + acquires;\n            if (nextc &lt; 0)\n                   \/\/ \u6ea2\u51fa\u3002\u52a0\u9501\u6b21\u6570\u4ece0\u5f00\u59cb\uff0c\u52a0\u9501\u4e0e\u91ca\u653e\u64cd\u4f5c\u662f\u5bf9\u79f0\u7684\uff0c\n                   \/\/ \u6240\u4ee5\u7edd\u4e0d\u4f1a\u662f\u5c0f\u4e8e0\u503c\uff0c\u5c0f\u4e8e0\u53ea\u80fd\u662f\u6ea2\u51fa\u3002\n                throw new Error(\"Maximum lock count exceeded\");\n\n            \/\/ \u9501\u88ab\u6301\u6709\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u6301\u6709\u8005\u624d\u80fd\u66f4\u65b0\u9501\u4fdd\u62a4\u7684\u8d44\u6e90\uff0c\n            \/\/ \u6240\u4ee5\u8fd9\u91cc\u4e0d\u9700\u8981\u7528CAS\u3002\n            setState(nextc);\n            return true ;\n        }\n        return false ;\n    }\n\n    protected final boolean tryRelease(int releases) {\n      \/\/ \u5148\u8bfb\u53d6state\u662f\u4e3a\u4e86\u83b7\u5f97\u4e00\u4e2a\u8bfb\u5c4f\u969c\uff0cowner\u4e0d\u662fvolatile\u7684\u3002\n        int c = getState() - releases;\n\n        \/\/ \u53ea\u6709\u9501\u7684\u6301\u6709\u8005\u624d\u80fd\u91ca\u653e\u9501\n        if (Thread.currentThread() != getExclusiveOwnerThread())\n            throw new IllegalMonitorStateException();\n        boolean free = false;\n        if (c == 0) {\/\/ \u9501\u91cd\u5165\u8ba1\u6570\u51cf\u52300\uff0c\u9700\u8981\u771f\u6b63\u91ca\u653e\u9501\u4e86\u3002\n            free = true;\n            setExclusiveOwnerThread( null);\n        }\n    \/\/ \u5982\u679cc\u4e3a0\uff0c\u5199\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c31\u4f1a\u770b\u5230\u9501\u88ab\u91ca\u653e\u4e86\uff0c\n       \/\/ \u6240\u4ee5setExclusiveOwnerThread\u5fc5\u987b\u5728\u8fd9\u4e2a\u5199\u4e4b\u524d\u5b8c\u6210\u3002\n        setState(c);\n        return free;\n    }\n\n    protected final boolean isHeldExclusively() {\n        \/\/ While we must in general read state before owner,\n        \/\/ we don't need to do so to check if current thread is owner\n        return getExclusiveOwnerThread() == Thread.currentThread();\n    }\n\n    final ConditionObject newCondition() {\n        return new ConditionObject();\n    }\n\n    \/\/ Methods relayed from outer class\n    final Thread getOwner() {\n        return getState() == 0 ? null : getExclusiveOwnerThread();\n    }\n\n    final int getHoldCount() {\n        return isHeldExclusively() ? getState() : 0; \/\/ \u4ee5state\u5c5e\u6027\u4f5c\u4e3a\u52a0\u9501\u6b21\u6570\u3002\n    }\n\n    final boolean isLocked() {\n        return getState() != 0;     \/\/ \u52a0\u9501\u6b21\u6570\u4e3a0\u8868\u793a\u6ca1\u6709\u88ab\u62e5\u6709\n    }\n\n    private void readObject(java.io.ObjectInputStream s)\n        throws java.io.IOException, ClassNotFoundException {\n        s.defaultReadObject();\n        setState(0); \/\/ reset to unlocked state\n    }\n}\n<\/code><\/pre>\n<h3>ReentrantLock.FairSync<\/h3>\n<p>\u63d0\u4f9b\u516c\u5e73\u6027\u7684\u9501\u5b9e\u73b0\u3002\u5b9e\u73b0\u516c\u5e73\u6027\u7684\u5173\u952e\u5728\u4e8e\uff1a\u5982\u679c\u9501\u88ab\u5360\u7528\u4e14\u5f53\u524d\u7ebf\u7a0b\u4e0d\u662f\u6301\u6709\u8005\u4e5f\u4e0d\u662f\u7b49\u5f85\u961f\u5217\u7684\u7b2c\u4e00\u4e2a\uff0c\u5219\u8fdb\u5165\u7b49\u5f85\u961f\u5217\u3002<\/p>\n<pre><code class=\"java\">static final class FairSync extends Sync {\n    private static final long serialVersionUID = -3000897897090466540L;\n\n    final void lock() {\n        acquire(1); \/\/acquire\u4f1a\u9996\u5148\u8c03\u7528tryAcquire\uff0c\u6240\u4ee5\u516c\u5e73\u7b56\u7565\u7684\u63a7\u5236\u7559\u7ed9tryAcquire\u3002\n    }\n\n    \/\/ tryAcquire\u7684\u516c\u5e73\u7248\u672c\u3002\u9664\u975e\u662f\u9012\u5f52\u8c03\u7528\u6216\u6ca1\u6709\u7b49\u5f85\u8005\u6216\u8005\u662f\u7b2c\u4e00\u4e2a\uff0c\u5426\u5219\u4e0d\u6388\u4e88\u8bbf\u95ee\u3002\n    protected final boolean tryAcquire(int acquires) {\n        final Thread current = Thread.currentThread();\n        int c = getState(); \/\/ \u8bfb\u5173\u5361\n        if (c == 0) {\n             \/\/ \u9501\u7a7a\u95f2\n            if (!hasQueuedPredecessors() &amp;&amp;     \/\/ \u6ca1\u6709\u7b49\u5f85\u7ed3\u70b9\uff0c\u4e5f\u5c31\u662f\u7b2c\u4e00\u4e2a\n                compareAndSetState(0, acquires)) { \/\/ \u5c1d\u8bd5\u83b7\u53d6\n                setExclusiveOwnerThread(current); \/\/ \u83b7\u53d6\u6210\u529f\uff0c\u8bbe\u4e3a\u6301\u6709\u8005\n                return true ;\n            }\n        }\n        else if (current == getExclusiveOwnerThread()) {\n             \/\/ \u91cd\u5165\n            int nextc = c + acquires;     \/\/ \u91cd\u5165\u6b21\u6570\u589e\u52a0\n            if (nextc &lt; 0)\/\/ \u6ea2\u51fa\uff0c\u91cd\u5165\u6b21\u6570\u592a\u591a\uff0c\u5728\u6539\u53d8\u72b6\u6001\u4e4b\u524d\u629b\u51fa\u5f02\u5e38\u4ee5\u786e\u4fdd\u9501\u7684\u72b6\u6001\u662f\u6b63\u786e\u7684\u3002\n                throw new Error(\"Maximum lock count exceeded\");\n            setState(nextc);\n            return true ;\n        }\n        return false ;\n    }\n}\n<\/code><\/pre>\n<h3>ReentrantLock.NonfairSync<\/h3>\n<p>\u63d0\u4f9b\u975e\u516c\u5e73\u6027\u7684\u9501\u5b9e\u73b0\u3002<\/p>\n<pre><code class=\"java\">static final class NonfairSync extends Sync {\n    private static final long serialVersionUID = 7316153563782823691L;\n\n    \/\/ \u6267\u884clock\u3002\u5c1d\u8bd5\u7acb\u5373\u95ef\u5165\uff0c\u5931\u8d25\u5c31\u9000\u56de\u5e38\u89c4\u6d41\u7a0b\n    final void lock() {\n        if (compareAndSetState(0, 1))\/\/ \u9996\u5148\u8fdb\u884c\u83b7\u53d6\n             \/\/ \u83b7\u53d6\u6210\u529f\uff0c\u628a\u5f53\u524d\u7ebf\u7a0b\u8bbe\u4e3a\u6301\u6709\u8005\n            setExclusiveOwnerThread(Thread.currentThread());\n        else\n             \/\/ \u83b7\u53d6\u5931\u8d25\uff0c\u8fdb\u5165\u5e38\u89c4\u6d41\u7a0b\uff1aacquire\u4f1a\u9996\u5148\u8c03\u7528tryAcquire\uff0c\n            acquire(1);\n    }\n\n    protected final boolean tryAcquire(int acquires) {\n        return nonfairTryAcquire(acquires);\n    }\n}\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u6ce8\u610f\u5230\u4ee3\u7801\u5b9e\u73b0\u91cc\u7684 <code>acquire(1);  sync.release(1);<\/code>\uff0c\u8fd9\u91cc\u90fd\u662f1\uff0c\u56e0\u4e3a\u52a0\u9501\u4e0e\u9000\u51fa\u7684\u8ba1\u6570\u6b65\u957f\u7684\u7edd\u5bf9\u503c\u662f\u5fc5\u987b\u4e00\u6837\u7684\u3002\u53ef\u4ee5\u628a1\u6362\u4e3a2\uff0c\u4f46\u8fd9\u6837\u4f1a\u51cf\u5c11\u9501\u53ef\u4ee5\u91cd\u5165\u7684\u6b21\u6570\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u6587\u7ae0\u5df2\u5b8c\u6574\u5c55\u793a\u4e86\u7b49\u5f85\u961f\u5217\u7684\u7ba1\u7406\uff08\u6dfb\u52a0\u7ed3\u70b9\u3001\u79fb\u9664\u53d6\u6d88\u7ed3\u70b9\uff09\u3001\u72ec\u5360\u6a21\u5f0f\u4e0b\u7684ac &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20131209\/618\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[122,231,110,124],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/618"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=618"}],"version-history":[{"count":5,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/618\/revisions"}],"predecessor-version":[{"id":813,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/618\/revisions\/813"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}