{"id":37283,"date":"2023-12-11T14:40:52","date_gmt":"2023-12-11T06:40:52","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37283"},"modified":"2023-12-11T14:40:52","modified_gmt":"2023-12-11T06:40:52","slug":"aqs-%e5%af%b9%e8%b5%84%e6%ba%90%e7%9a%84%e5%85%b1%e4%ba%ab%e6%96%b9%e5%bc%8f-%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/aqs-%e5%af%b9%e8%b5%84%e6%ba%90%e7%9a%84%e5%85%b1%e4%ba%ab%e6%96%b9%e5%bc%8f-%ef%bc%9f\/","title":{"rendered":"AQS \u5bf9\u8d44\u6e90\u7684\u5171\u4eab\u65b9\u5f0f \uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    AQS\u5b9a\u4e49\u4e24\u79cd\u8d44\u6e90\u5171\u4eab\u65b9\u5f0f**<\/p>\n<p>&lt;strong&gt;1)Exclusive&lt;\/strong&gt;\uff08\u72ec\u5360\uff09<\/p>\n<p>\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u6267\u884c\uff0c\u5982ReentrantLock\u3002\u53c8\u53ef\u5206\u4e3a\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501,ReentrantLock \u540c\u65f6\u652f\u6301\u4e24\u79cd\u9501,\u4e0b\u9762\u4ee5 ReentrantLock \u5bf9\u8fd9\u4e24\u79cd\u9501\u7684\u5b9a\u4e49\u505a\u4ecb\u7ecd\uff1a&lt;\/p&gt;<\/p>\n<p>&lt;ul&gt;&lt;li&gt;\u516c\u5e73\u9501\uff1a\u6309\u7167\u7ebf\u7a0b\u5728\u961f\u5217\u4e2d\u7684\u6392\u961f\u987a\u5e8f\uff0c\u5148\u5230\u8005\u5148\u62ff\u5230\u9501&lt;\/li&gt;&lt;li&gt;\u975e\u516c\u5e73\u9501\uff1a\u5f53\u7ebf\u7a0b\u8981\u83b7\u53d6\u9501\u65f6\uff0c\u5148\u901a\u8fc7\u4e24\u6b21 CAS \u64cd\u4f5c\u53bb\u62a2\u9501\uff0c\u5982\u679c\u6ca1\u62a2\u5230\uff0c\u5f53\u524d\u7ebf\u7a0b\u518d\u52a0\u5165\u5230\u961f\u5217\u4e2d\u7b49\u5f85\u5524\u9192\u3002&lt;\/li&gt;&lt;\/ul&gt;<\/p>\n<p>&lt;blockquote&gt;\u8bf4\u660e\uff1a\u4e0b\u9762\u8fd9\u90e8\u5206\u5173\u4e8e &lt;code&gt;ReentrantLock&lt;\/code&gt; \u6e90\u4ee3\u7801\u5185\u5bb9\u8282\u9009\u81ea\uff1ahttps:\/\/www.javadoop.com\/post\/AbstractQueuedSynchronizer-2\uff0c\u8fd9\u662f\u4e00\u7bc7\u5f88\u4e0d\u9519\u6587\u7ae0\uff0c\u63a8\u8350\u9605\u8bfb\u3002&lt;\/p&gt;&lt;\/blockquote&gt;<\/p>\n<p>&lt;p&gt;&lt;strong&gt;\u4e0b\u9762\u6765\u770b ReentrantLock \u4e2d\u76f8\u5173\u7684\u6e90\u4ee3\u7801\uff1a&lt;\/strong&gt;<\/p>\n<p>ReentrantLock \u9ed8\u8ba4\u91c7\u7528\u975e\u516c\u5e73\u9501\uff0c\u56e0\u4e3a\u8003\u8651\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff0c\u901a\u8fc7 boolean \u6765\u51b3\u5b9a\u662f\u5426\u7528\u516c\u5e73\u9501\uff08\u4f20\u5165 true \u7528\u516c\u5e73\u9501\uff09\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;java\/** Synchronizer providing all implementation mechanics *\/private final Sync sync;public ReentrantLock() {    \/\/ \u9ed8\u8ba4\u975e\u516c\u5e73\u9501    sync = new NonfairSync();}public ReentrantLock(boolean fair) {    sync = fair ? new FairSync() : new NonfairSync();}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>ReentrantLock \u4e2d\u516c\u5e73\u9501\u7684 &lt;code&gt;lock&lt;\/code&gt; \u65b9\u6cd5<\/p>\n<p>&lt;pre&gt;&lt;code&gt;javastatic final class FairSync extends Sync {    final void lock() {        acquire(1);    }    \/\/ AbstractQueuedSynchronizer.acquire(int arg)    public final void acquire(int arg) {        if (!tryAcquire(arg) &amp;&amp;            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))            selfInterrupt();    }    protected final boolean tryAcquire(int acquires) {        final Thread current = Thread.currentThread();        int c = getState();        if (c == 0) {            \/\/ 1. \u548c\u975e\u516c\u5e73\u9501\u76f8\u6bd4\uff0c\u8fd9\u91cc\u591a\u4e86\u4e00\u4e2a\u5224\u65ad\uff1a\u662f\u5426\u6709\u7ebf\u7a0b\u5728\u7b49\u5f85            if (!hasQueuedPredecessors() &amp;&amp;                compareAndSetState(0, acquires)) {                setExclusiveOwnerThread(current);                return true;            }        }        else if (current == getExclusiveOwnerThread()) {            int nextc = c + acquires;            if (nextc &lt; 0)                throw new Error(&quot;&quot;Maximum lock count exceeded&quot;&quot;);            setState(nextc);            return true;        }        return false;    }}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u975e\u516c\u5e73\u9501\u7684 lock \u65b9\u6cd5\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;javastatic final class NonfairSync extends Sync {    final void lock() {        \/\/ 2. \u548c\u516c\u5e73\u9501\u76f8\u6bd4\uff0c\u8fd9\u91cc\u4f1a\u76f4\u63a5\u5148\u8fdb\u884c\u4e00\u6b21CAS\uff0c\u6210\u529f\u5c31\u8fd4\u56de\u4e86        if (compareAndSetState(0, 1))            setExclusiveOwnerThread(Thread.currentThread());        else            acquire(1);    }    \/\/ AbstractQueuedSynchronizer.acquire(int arg)    public final void acquire(int arg) {        if (!tryAcquire(arg) &amp;&amp;            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))            selfInterrupt();    }    protected final boolean tryAcquire(int acquires) {        return nonfairTryAcquire(acquires);    }}\/** * Performs non-fair tryLock.  tryAcquire is implemented in * subclasses, but both need nonfair try for trylock method. *\/final boolean nonfairTryAcquire(int acquires) {    final Thread current = Thread.currentThread();    int c = getState();    if (c == 0) {        \/\/ \u8fd9\u91cc\u6ca1\u6709\u5bf9\u963b\u585e\u961f\u5217\u8fdb\u884c\u5224\u65ad        if (compareAndSetState(0, acquires)) {            setExclusiveOwnerThread(current);            return true;        }    }    else if (current == getExclusiveOwnerThread()) {        int nextc = c + acquires;        if (nextc &lt; 0) \/\/ overflow            throw new Error(&quot;&quot;Maximum lock count exceeded&quot;&quot;);        setState(nextc);        return true;    }    return false;}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u603b\u7ed3\uff1a\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501\u53ea\u6709\u4e24\u5904\u4e0d\u540c\uff1a<\/p>\n<p>&lt;ol&gt;&lt;li&gt;\u975e\u516c\u5e73\u9501\u5728\u8c03\u7528 lock \u540e\uff0c\u9996\u5148\u5c31\u4f1a\u8c03\u7528 CAS \u8fdb\u884c\u4e00\u6b21\u62a2\u9501\uff0c\u5982\u679c\u8fd9\u4e2a\u65f6\u5019\u6070\u5de7\u9501\u6ca1\u6709\u88ab\u5360\u7528\uff0c\u90a3\u4e48\u76f4\u63a5\u5c31\u83b7\u53d6\u5230\u9501\u8fd4\u56de\u4e86\u3002&lt;\/li&gt;&lt;li&gt;\u975e\u516c\u5e73\u9501\u5728 CAS \u5931\u8d25\u540e\uff0c\u548c\u516c\u5e73\u9501\u4e00\u6837\u90fd\u4f1a\u8fdb\u5165\u5230 tryAcquire \u65b9\u6cd5\uff0c\u5728 tryAcquire \u65b9\u6cd5\u4e2d\uff0c\u5982\u679c\u53d1\u73b0\u9501\u8fd9\u4e2a\u65f6\u5019\u88ab\u91ca\u653e\u4e86\uff08state == 0\uff09\uff0c\u975e\u516c\u5e73\u9501\u4f1a\u76f4\u63a5 CAS \u62a2\u9501\uff0c\u4f46\u662f\u516c\u5e73\u9501\u4f1a\u5224\u65ad\u7b49\u5f85\u961f\u5217\u662f\u5426\u6709\u7ebf\u7a0b\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\uff0c\u5982\u679c\u6709\u5219\u4e0d\u53bb\u62a2\u9501\uff0c\u4e56\u4e56\u6392\u5230\u540e\u9762\u3002&lt;\/li&gt;&lt;\/ol&gt;<\/p>\n<p>\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501\u5c31\u8fd9\u4e24\u70b9\u533a\u522b\uff0c\u5982\u679c\u8fd9\u4e24\u6b21 CAS \u90fd\u4e0d\u6210\u529f\uff0c\u90a3\u4e48\u540e\u9762\u975e\u516c\u5e73\u9501\u548c\u516c\u5e73\u9501\u662f\u4e00\u6837\u7684\uff0c\u90fd\u8981\u8fdb\u5165\u5230\u963b\u585e\u961f\u5217\u7b49\u5f85\u5524\u9192\u3002<\/p>\n<p>\u76f8\u5bf9\u6765\u8bf4\uff0c\u975e\u516c\u5e73\u9501\u4f1a\u6709\u66f4\u597d\u7684\u6027\u80fd\uff0c\u56e0\u4e3a\u5b83\u7684\u541e\u5410\u91cf\u6bd4\u8f83\u5927\u3002\u5f53\u7136\uff0c\u975e\u516c\u5e73\u9501\u8ba9\u83b7\u53d6\u9501\u7684\u65f6\u95f4\u53d8\u5f97\u66f4\u52a0\u4e0d\u786e\u5b9a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5728\u963b\u585e\u961f\u5217\u4e2d\u7684\u7ebf\u7a0b\u957f\u671f\u5904\u4e8e\u9965\u997f\u72b6\u6001\u3002<\/p>\n<p>&lt;strong&gt;2)Share&lt;\/strong&gt;\uff08\u5171\u4eab\uff09<\/p>\n<p>\u591a\u4e2a\u7ebf\u7a0b\u53ef\u540c\u65f6\u6267\u884c\uff0c\u5982Semaphore\/CountDownLatch\u3002Semaphore\u3001CountDownLatCh\u3001 CyclicBarrier\u3001ReadWriteLock \u6211\u4eec\u90fd\u4f1a\u5728\u540e\u9762\u8bb2\u5230\u3002<\/p>\n<p>ReentrantReadWriteLock \u53ef\u4ee5\u770b\u6210\u662f\u7ec4\u5408\u5f0f\uff0c\u56e0\u4e3aReentrantReadWriteLock\u4e5f\u5c31\u662f\u8bfb\u5199\u9501\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u67d0\u4e00\u8d44\u6e90\u8fdb\u884c\u8bfb\u3002<\/p>\n<p>\u4e0d\u540c\u7684\u81ea\u5b9a\u4e49\u540c\u6b65\u5668\u4e89\u7528\u5171\u4eab\u8d44\u6e90\u7684\u65b9\u5f0f\u4e5f\u4e0d\u540c\u3002\u81ea\u5b9a\u4e49\u540c\u6b65\u5668\u5728\u5b9e\u73b0\u65f6\u53ea\u9700\u8981\u5b9e\u73b0\u5171\u4eab\u8d44\u6e90 state \u7684\u83b7\u53d6\u4e0e\u91ca\u653e\u65b9\u5f0f\u5373\u53ef\uff0c\u81f3\u4e8e\u5177\u4f53\u7ebf\u7a0b\u7b49\u5f85\u961f\u5217\u7684\u7ef4\u62a4\uff08\u5982\u83b7\u53d6\u8d44\u6e90\u5931\u8d25\u5165\u961f\/\u5524\u9192\u51fa\u961f\u7b49\uff09\uff0cAQS\u5df2\u7ecf\u5728\u4e0a\u5c42\u5df2\u7ecf\u5e2e\u6211\u4eec\u5b9e\u73b0\u597d\u4e86\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; AQS\u5b9a\u4e49\u4e24\u79cd\u8d44\u6e90\u5171\u4eab\u65b9\u5f0f** &lt;strong&gt;1)Exclusi [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37283","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=37283"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37283\/revisions"}],"predecessor-version":[{"id":37284,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37283\/revisions\/37284"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37283"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}