{"id":4137,"date":"2024-05-22T08:01:34","date_gmt":"2024-05-22T00:01:34","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e8%a7%a3%e9%87%8a%e4%bf%a1%e5%8f%b7%e9%87%8f%ef%bc%88semaphore%ef%bc%89%e4%b8%8e%e4%ba%92%e6%96%a5%e9%87%8f%ef%bc%88mutex%ef%bc%89%e7%9a%84%e5%8c%ba%e5%88%ab\/"},"modified":"2024-08-02T17:04:39","modified_gmt":"2024-08-02T09:04:39","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e8%a7%a3%e9%87%8a%e4%bf%a1%e5%8f%b7%e9%87%8f%ef%bc%88semaphore%ef%bc%89%e4%b8%8e%e4%ba%92%e6%96%a5%e9%87%8f%ef%bc%88mutex%ef%bc%89%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e8%a7%a3%e9%87%8a%e4%bf%a1%e5%8f%b7%e9%87%8f%ef%bc%88semaphore%ef%bc%89%e4%b8%8e%e4%ba%92%e6%96%a5%e9%87%8f%ef%bc%88mutex%ef%bc%89%e7%9a%84%e5%8c%ba%e5%88%ab\/","title":{"rendered":"\u9762\u8bd5\u5b98\uff1a\u89e3\u91ca\u4fe1\u53f7\u91cf\uff08Semaphore\uff09\u4e0e\u4e92\u65a5\u91cf\uff08Mutex\uff09\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<div class=\"wxsyncmain\">\n<p data-v-md-line=\"1\"><span style=\"font-size: 16px; letter-spacing: normal;\">\u4fe1\u53f7\u91cf\uff08Semaphore\uff09\u548c\u4e92\u65a5\u91cf\uff08Mutex\uff09\u90fd\u662f\u7528\u4e8e\u63a7\u5236\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u4ee5\u907f\u514d\u5e76\u53d1\u51b2\u7a81\u7684\u540c\u6b65\u673a\u5236\uff0c\u4f46\u5b83\u4eec\u5728\u6982\u5ff5\u548c\u7528\u9014\u4e0a\u6709\u6240\u533a\u522b\u3002<\/span><\/p>\n<p style=\"margin-top: 24px; margin-bottom: 24px;\"><strong><span style=\"font-size: 20px;\">\u4fe1\u53f7\u91cf\uff08Semaphore\uff09\uff1a<\/span><\/strong><\/p>\n<ul class=\"list-paddingleft-1\" data-v-md-line=\"4\">\n<li><span style=\"font-size: 16px; letter-spacing: normal;\">\u4fe1\u53f7\u91cf\u53ef\u4ee5\u6709\u591a\u4e2a\u8bb8\u53ef\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u53ef\u4ee5\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u8d44\u6e90\u6216\u8d44\u6e90\u6c60\u3002<\/span><\/li>\n<li><span style=\"font-size: 16px; letter-spacing: normal;\">\u4fe1\u53f7\u91cf\u4e3b\u8981\u7528\u4e8e\u9650\u5236\u5bf9\u8d44\u6e90\u7684\u8bbf\u95ee\u6570\u91cf\uff0c\u4e0d\u4e00\u5b9a\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u9700\u8981\u5f00\u53d1\u8005\u81ea\u5df1\u63a7\u5236\u3002<\/span><\/li>\n<li><span style=\"font-size: 16px; letter-spacing: normal;\">\u4fe1\u53f7\u91cf\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u8d44\u6e90\u6c60\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff0c\u6216\u9650\u5236\u5bb9\u5668\u7684\u5bb9\u91cf\uff0c\u5982Semaphore\u53ef\u7528\u4e8e\u9650\u5236\u6267\u884c\u7684\u7ebf\u7a0b\u6570\u76ee\u3002<\/span><\/li>\n<\/ul>\n<p style=\"margin-top: 24px; margin-bottom: 24px;\" data-v-md-line=\"8\"><strong><span style=\"font-size: 20px;\">\u4e92\u65a5\u91cf\uff08Mutex\uff09\uff1a<\/span><\/strong><\/p>\n<ul class=\"list-paddingleft-1\" data-v-md-line=\"9\">\n<li><span style=\"font-size: 16px; letter-spacing: normal;\">\u4e92\u65a5\u91cf\u662f\u4e00\u4e2a\u7279\u6b8a\u7c7b\u578b\u7684\u4fe1\u53f7\u91cf\uff0c\u4e00\u6b21\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\uff0c\u5373\u5b83\u7684\u8bb8\u53ef\u6570\u91cf\u9650\u5236\u4e3a1\u3002<\/span><\/li>\n<li><span style=\"font-size: 16px; letter-spacing: normal;\">\u4e92\u65a5\u91cf\u63d0\u4f9b\u4e86\u6240\u6709\u6743\u7684\u6982\u5ff5\uff0c\u53ea\u6709\u9501\u7684\u6301\u6709\u8005\u80fd\u591f\u91ca\u653e\u9501\uff0c\u800c\u4fe1\u53f7\u91cf\u5219\u6ca1\u6709\u6240\u6709\u6743\u7684\u6982\u5ff5\uff0c\u4efb\u4f55\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u91ca\u653e\u3002<\/span><\/li>\n<li><span style=\"font-size: 16px; letter-spacing: normal;\">\u4e92\u65a5\u91cf\u901a\u5e38\u7528\u4e8e\u4fdd\u62a4\u4e34\u754c\u533a\uff0c\u786e\u4fdd\u67d0\u4e00\u65f6\u95f4\u70b9\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8fdb\u5165\u4e34\u754c\u533a\u4fee\u6539\u5171\u4eab\u6570\u636e\u3002<\/span><\/li>\n<\/ul>\n<p style=\"margin-top: 24px; margin-bottom: 24px;\"><strong><span style=\"font-size: 20px;\">\u4f8b\u5b50\uff1a<\/span><\/strong><\/p>\n<p style=\"margin-top: 16px; margin-bottom: 16px;\"><span style=\"font-size: 16px; letter-spacing: normal;\">\u4fe1\u53f7\u91cf\uff08Semaphore\uff09\u793a\u4f8b:<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<div>\n<pre>import java.util.concurrent.Semaphore;\n\npublic class ConnectionPool {\n    private static final int MAX_AVAILABLE = 10;\n    private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);\n\n    public Object getConnection() throws InterruptedException {\n        available.acquire();\n        try {\n            \/\/ \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u64cd\u4f5c... \n            return fetchConnection();\n        } finally {\n            available.release();\n        }\n    } \/\/ \u91ca\u653e\u6570\u636e\u5e93\u8fde\u63a5\u56de\u6c60\u7684\u65b9\u6cd5...\n\n    public void releaseConnection(Object conn) {\n        \/\/ \u91ca\u653e\u64cd\u4f5c... \n        available.release();\n    }\n\n    private Object fetchConnection() {\n        \/\/ \u5b9e\u9645\u521b\u5efa\u6216\u91cd\u7528\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u7684\u4ee3\u7801... \n    }\n}<\/pre>\n<\/div>\n<\/section>\n<section style=\"margin-bottom: 16px;\"><span style=\"font-size: 16px; letter-spacing: normal;\">\u6b64\u793a\u4f8b\u4e2d\uff0cSemaphore\u8bbe\u5b9a\u670910\u4e2a\u8bb8\u53ef\uff0c\u4ee3\u8868\u4e00\u4e2a\u80fd\u5bb9\u7eb310\u4e2a\u8fde\u63a5\u7684\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u3002<\/span><\/section>\n<p style=\"margin-top: 16px; margin-bottom: 16px;\"><span style=\"font-size: 16px; letter-spacing: normal;\">\u4e92\u65a5\u91cf\uff08Mutex\uff09\u793a\u4f8b\uff0c\u901a\u5e38\u5728Java\u4e2d\u901a\u8fc7ReentrantLock\u5b9e\u73b0\uff1a<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<div>\n<pre>import java.util.concurrent.locks.ReentrantLock;\n\npublic class Counter {\n    private int count = 0;\n    private final ReentrantLock lock = new ReentrantLock();\n\n    public void increment() {\n        lock.lock(); \/\/ \u83b7\u53d6\u9501 \n        try {\n            count++;\n        } finally {\n            lock.unlock();\n            \/\/ \u91ca\u653e\u9501\n        }\n    }\n\n    public int getCount() {\n        return count;\n    }\n}<\/pre>\n<\/div>\n<\/section>\n<p data-v-md-line=\"68\"><span style=\"font-size: 16px; letter-spacing: normal;\">\u8be5\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ReentrantLock\u6765\u4fdd\u8bc1\u5728\u4efb\u4f55\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u591f\u4fee\u6539count\u53d8\u91cf\u7684\u503c\u3002\u5728increment()\u65b9\u6cd5\u4e2d\uff0c\u5f53\u7ebf\u7a0b\u8c03\u7528lock.lock()\u540e\uff0c\u5176\u4ed6\u5c1d\u8bd5\u8c03\u7528increment()\u7684\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e\u76f4\u5230\u9501\u88ab\u91ca\u653e\u3002<\/span><\/p>\n<p data-v-md-line=\"70\"><strong><span style=\"font-size: 16px; letter-spacing: normal;\"><span style=\"font-size: 16px; letter-spacing: normal; text-wrap: wrap;\">\u603b\u7ed3<\/span><\/span><\/strong><span style=\"font-size: 16px; letter-spacing: normal;\"><span style=\"font-size: 16px; letter-spacing: normal; text-wrap: wrap;\">\uff1a<\/span>\u4fe1\u53f7\u91cf\u7528\u4e8e\u63a7\u5236\u540c\u65f6\u8bbf\u95ee\u8d44\u6e90\u7684\u7ebf\u7a0b\u6570\u91cf\uff0c\u800c\u4e92\u65a5\u91cf\u7528\u4e8e\u4fdd\u8bc1\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u8d44\u6e90\u3002<\/span><\/p>\n<p style=\"display: none;\">\n<blockquote><p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7: \u4e92\u8054\u7f51\u9762\u8bd5\u5c0f\u5e2e\u624b<\/p><\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u4fe1\u53f7\u91cf\uff08Semaphore\uff09\u548c\u4e92\u65a5\u91cf\uff08Mutex\uff09\u90fd\u662f\u7528\u4e8e\u63a7\u5236\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u4ee5\u907f\u514d\u5e76\u53d1\u51b2\u7a81\u7684\u540c\u6b65\u673a\u5236\uff0c\u4f46\u5b83\u4eec\u5728\u6982\u5ff5\u548c\u7528\u9014\u4e0a\u6709\u6240\u533a &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4134,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[30],"tags":[],"class_list":["post-4137","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-30"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145427426-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145427426-300x127.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145427426.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145427426-300x127.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145427426.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145427426.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-05-22","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":341,"next_post_id":4759,"next_post_title":"\u7f8e\u56e2\u4e00\u9762\uff1a\u78b0\u5230\u8fc7OOM\u5417\uff1f\u4f60\u662f\u600e\u4e48\u5904\u7406\u7684\uff1f","previous_post_id":4133,"previous_post_title":"HashSet\u3001LinkedHashSet\u548cTreeSet\u4e4b\u95f4\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4137"}],"version-history":[{"count":2,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4137\/revisions"}],"predecessor-version":[{"id":6471,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4137\/revisions\/6471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4134"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}