{"id":888,"date":"2014-04-14T22:53:25","date_gmt":"2014-04-14T14:53:25","guid":{"rendered":"http:\/\/coderbee.net\/?p=888"},"modified":"2014-05-03T23:04:29","modified_gmt":"2014-05-03T15:04:29","slug":"juc-semaphore-%e4%bf%a1%e5%8f%b7%e9%87%8f","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20140414\/888","title":{"rendered":"JUC Semaphore \u4fe1\u53f7\u91cf"},"content":{"rendered":"<h3>\u6982\u8981<\/h3>\n<p>\u4e00\u4e2a\u8ba1\u6570\u4fe1\u53f7\u91cf\uff0c\u7ef4\u62a4\u4e86\u4e00\u7ec4\u8bb8\u53ef\u3002<code>acquire()<\/code> \u65b9\u6cd5\u5728\u8bb8\u53ef\u53ef\u7528\u524d\u5c06\u963b\u585e\uff0c\u8bb8\u53ef\u53ef\u7528\u65f6\u83b7\u53d6\uff0c\u6bcf\u4e2a<code>release()<\/code> \u65b9\u6cd5\u6dfb\u52a0\u4e00\u4e2a\u8bb8\u53ef\uff0c\u6f5c\u5728\u5730\u91ca\u653e\u4e00\u4e2a\u963b\u585e\u7684\u83b7\u53d6\u7ebf\u7a0b\u3002<\/p>\n<p>\u4fe1\u53f7\u91cf\u5e38\u7528\u4e8e\u7ea6\u675f\u8bbf\u95ee\u4e00\u4e9b\uff08\u7269\u7406\u6216\u903b\u8f91\uff09\u8d44\u6e90\u7684\u7ebf\u7a0b\u6570\u91cf\u3002<\/p>\n<p>\u4fe1\u53f7\u91cf\u521d\u59cb\u5316\u4e3a 1 \u53ef\u4ee5\u7528\u4f5c\u4e92\u65a5\u9501\uff0c\u66f4\u5e38\u89c1\u7684\u662f\u79f0\u4e3a\u4e8c\u8fdb\u5236\u4fe1\u53f7\u91cf\uff0c\u56e0\u4e3a\u5b83\u53ea\u6709\u4e24\u4e2a\u72b6\u6001\uff1a\u6709\u4e00\u4e2a\u8bb8\u53ef\u53ef\u7528\uff0c\u6216\u6ca1\u6709\u8bb8\u53ef\u53ef\u7528\u3002\u5f53\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u65f6\uff0c\u4e8c\u8fdb\u5236\u4fe1\u53f7\u91cf\u6709\u4e2a\u5c5e\u6027\uff0c\u201c\u9501\u201d\u53ef\u4ee5\u88ab\u5176\u4ed6\u7ebf\u7a0b\u800c\u4e0d\u662f\u5c5e\u4e3b\u7ebf\u7a0b\uff08\u4fe1\u53f7\u91cf\u6ca1\u6709\u8bb0\u5f55\u5c5e\u4e3b\u5173\u7cfb\uff09\u91ca\u653e\u3002<\/p>\n<p>\u516c\u5e73\u6027\u5c5e\u6027\uff1a\u516c\u5e73\u7b56\u7565\uff0c\u6309FIFO\u987a\u5e8f\u5206\u914d\u8bb8\u53ef\uff1b\u975e\u516c\u5e73\u7b56\u7565\uff0c\u5141\u8bb8\u95ef\u5165\uff0c\u5373 <code>acquire<\/code> \u8c03\u7528\u7ebf\u7a0b\u83b7\u53d6\u8bb8\u53ef\uff0c\u800c\u4e0d\u662f\u5df2\u7ecf\u5728\u7b49\u5f85\u7684\u7ebf\u7a0b\u3002<\/p>\n<p>\u4e00\u822c\u5730\uff0c\u4fe1\u53f7\u91cf\u7528\u4e8e\u63a7\u5236\u8d44\u6e90\u7684\u8bbf\u95ee\u5e94\u5f53\u521d\u59cb\u5316\u4e3a\u516c\u5e73\u7684\uff0c\u6765\u4fdd\u8bc1\u6ca1\u6709\u7ebf\u7a0b\u56e0\u4e3a\u4e0d\u80fd\u8bbf\u95ee\u8d44\u6e90\u800c\u9965\u997f\u3002\u5f53\u628a\u4fe1\u53f7\u91cf\u7528\u4e8e\u5176\u4ed6\u540c\u6b65\u7c7b\u578b\u7684\u63a7\u5236\uff0c\u975e\u516c\u5e73\u987a\u5e8f\u7684\u541e\u5410\u91cf\u4f18\u52bf\u538b\u8fc7\u516c\u5e73\u7684\u8003\u91cf\u3002<\/p>\n<p>\u5185\u5b58\u4e00\u81f4\u6027\u6548\u679c\uff1a\u7ebf\u7a0b\u8c03\u7528 &#8220;release&#8221; \u4e4b\u524d\u7684\u52a8\u4f5c happens-before \u4e8e\u6210\u529f\u8c03\u7528 &#8220;acquire&#8221; \u7684\u5176\u4ed6\u7ebf\u7a0b\u7684\u540e\u7eed\u52a8\u4f5c\u3002<\/p>\n<p><!--more--><\/p>\n<h3>\u5b9e\u73b0<\/h3>\n<p>\u7531\u4e8e\u4fe1\u53f7\u91cf\u4e5f\u53ea\u662f\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u5b83\u53ef\u4ee5\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u8bb8\u53ef\uff0c\u5982\u679c\u6ca1\u6709\u8bb8\u53ef\u53ef\u7528\u5219\u963b\u585e\u83b7\u53d6\u7ebf\u7a0b\uff0c\u8fd9\u4e9b\u90fd\u8ddf\u5171\u4eab\u6a21\u5f0f\u4e0b\u7684AQS\u975e\u5e38\u543b\u5408\u3002<\/p>\n<p>\u7528AQS\u7684\u72b6\u6001\u6765\u8868\u793a\u8bb8\u53ef\u7684\u6570\u91cf\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u975e\u516c\u5e73\u7b56\u7565\u4e0b\u83b7\u53d6\u8bb8\u53ef\nfinal int nonfairTryAcquireShared( int acquires) {\n    for (;;) {\n        int available = getState();\n        int remaining = available - acquires;\n        if (remaining &lt; 0 ||\n            compareAndSetState(available, remaining))\n            return remaining;\n    }\n}\n\n\/\/ \u91ca\u653e\u8bb8\u53ef\nprotected final boolean tryReleaseShared (int releases) {\n    for (;;) {\n        int current = getState();\n        int next = current + releases;\n        if (next &lt; current) \/\/ overflow\n            throw new Error( \"Maximum permit count exceeded\");\n        if (compareAndSetState(current, next))\n            return true;\n    }\n}\n<\/code><\/pre>\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>\u6982\u8981 \u4e00\u4e2a\u8ba1\u6570\u4fe1\u53f7\u91cf\uff0c\u7ef4\u62a4\u4e86\u4e00\u7ec4\u8bb8\u53ef\u3002acquire() \u65b9\u6cd5\u5728\u8bb8\u53ef\u53ef\u7528\u524d\u5c06\u963b\u585e &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20140414\/888\">\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":[110,162,163],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/888"}],"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=888"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/888\/revisions"}],"predecessor-version":[{"id":890,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/888\/revisions\/890"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}