{"id":4112,"date":"2024-04-16T08:57:54","date_gmt":"2024-04-16T00:57:54","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e5%ad%97%e8%8a%82%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e4%bb%80%e4%b9%88%e6%98%af%e9%87%8d%e5%85%a5%e9%94%81%ef%bc%88reentrantlock%ef%bc%89%ef%bc%9f%e4%b8%8esynchronized%e5%85%b3%e9%94%ae%e5%ad%97\/"},"modified":"2024-04-16T08:57:54","modified_gmt":"2024-04-16T00:57:54","slug":"%e5%ad%97%e8%8a%82%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e4%bb%80%e4%b9%88%e6%98%af%e9%87%8d%e5%85%a5%e9%94%81%ef%bc%88reentrantlock%ef%bc%89%ef%bc%9f%e4%b8%8esynchronized%e5%85%b3%e9%94%ae%e5%ad%97","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e5%ad%97%e8%8a%82%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e4%bb%80%e4%b9%88%e6%98%af%e9%87%8d%e5%85%a5%e9%94%81%ef%bc%88reentrantlock%ef%bc%89%ef%bc%9f%e4%b8%8esynchronized%e5%85%b3%e9%94%ae%e5%ad%97\/","title":{"rendered":"\u5b57\u8282\u9762\u8bd5\u9898\uff1a\u4ec0\u4e48\u662f\u91cd\u5165\u9501\uff08ReentrantLock\uff09\uff1f\u4e0esynchronized\u5173\u952e\u5b57\u7684\u533a\u522b\uff1f"},"content":{"rendered":"<div class='wxsyncmain'>\n<p data-v-md-line=\"1\"><strong><span style=\"font-size: 15px;\">\u91cd\u5165\u9501<\/span><\/strong><span style=\"font-size: 15px;\">\uff08ReentrantLock\uff09\u662fJava\u4e2d\u7684\u4e00\u79cd\u663e\u5f0f\u9501\u5b9e\u73b0\uff0c\u5b83\u5141\u8bb8\u7ebf\u7a0b\u91cd\u590d\u83b7\u53d6\u540c\u4e00\u4e2a\u9501\u3002\u8fd9\u610f\u5473\u7740\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u591a\u6b21\u83b7\u5f97\u540c\u4e00\u4e2a\u9501\u800c\u4e0d\u88ab\u963b\u585e\uff0c\u4f7f\u5f97\u7ebf\u7a0b\u80fd\u591f\u4ee5\u9012\u5f52\u7684\u65b9\u5f0f\u8fdb\u5165\u7531\u540c\u4e00\u4e2a\u9501\u4fdd\u62a4\u7684\u4ee3\u7801\u5757\u3002<\/span><\/p>\n<p data-v-md-line=\"3\"><span style=\"font-size: 15px;\">\u4e0e<strong>synchronized<\/strong>\u5173\u952e\u5b57\u76f8\u6bd4\uff0cReentrantLock\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u529f\u80fd\u3002\u4e0b\u9762\u662fReentrantLock\u4e0esynchronized\u5173\u952e\u5b57\u7684\u4e3b\u8981\u533a\u522b\uff1a<\/span><\/p>\n<ol data-v-md-line=\"5\" class=\"list-paddingleft-1\">\n<li>\n<p data-v-md-line=\"5\"><strong><span style=\"font-size: 15px;\">\u53ef\u4e2d\u65ad\u6027<\/span><\/strong><span style=\"font-size: 15px;\">\uff1aReentrantLock\u63d0\u4f9b\u4e86lockInterruptibly\u65b9\u6cd5\uff0c\u5141\u8bb8\u7ebf\u7a0b\u5728\u7b49\u5f85\u9501\u7684\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u54cd\u5e94\u4e2d\u65ad\uff0c\u800csynchronized\u5173\u952e\u5b57\u4e0d\u652f\u6301\u8fd9\u6837\u7684\u64cd\u4f5c\u3002<\/span><\/p>\n<\/li>\n<li>\n<p data-v-md-line=\"7\"><strong><span style=\"font-size: 15px;\">\u516c\u5e73\u6027\u9009\u62e9<\/span><\/strong><span style=\"font-size: 15px;\">\uff1aReentrantLock\u53ef\u4ee5\u4f5c\u4e3a\u516c\u5e73\u9501\u4f7f\u7528\uff0c\u5373\u7b49\u5f85\u65f6\u95f4\u6700\u957f\u7684\u7ebf\u7a0b\u5c06\u83b7\u5f97\u9501\uff0c\u800csynchronized\u5219\u65e0\u6cd5\u6307\u5b9a\u662f\u5426\u4e3a\u516c\u5e73\u9501\u3002<\/span><\/p>\n<\/li>\n<li>\n<p data-v-md-line=\"9\"><strong><span style=\"font-size: 15px;\">\u6761\u4ef6\u53d8\u91cf\u652f\u6301<\/span><\/strong><span style=\"font-size: 15px;\">\uff1aReentrantLock\u53ef\u4ee5\u914d\u5408Condition\u5bf9\u8c61\u5b9e\u73b0\u7cbe\u786e\u5730\u7ebf\u7a0b\u7b49\u5f85\/\u901a\u77e5\u673a\u5236\uff0c\u800csynchronized\u65e0\u6cd5\u76f4\u63a5\u652f\u6301\u8fd9\u79cd\u529f\u80fd\u3002<\/span><\/p>\n<\/li>\n<li>\n<p data-v-md-line=\"11\"><strong><span style=\"font-size: 15px;\">\u5c1d\u8bd5\u83b7\u53d6\u9501<\/span><\/strong><span style=\"font-size: 15px;\">\uff1aReentrantLock\u63d0\u4f9b\u4e86tryLock\u65b9\u6cd5\uff0c\u80fd\u591f\u5c1d\u8bd5\u83b7\u53d6\u9501\u800c\u4e0d\u88ab\u963b\u585e\uff0c\u800csynchronized\u65e0\u6cd5\u5b9e\u73b0\u8fd9\u79cd\u975e\u963b\u585e\u5f0f\u7684\u9501\u83b7\u53d6\u3002<\/span><\/p>\n<\/li>\n<li>\n<p data-v-md-line=\"13\"><strong><span style=\"font-size: 15px;\">\u9501\u7684\u91ca\u653e<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u4f7f\u7528synchronized\u65f6\uff0c\u9501\u7684\u91ca\u653e\u7531JVM\u9690\u5f0f\u7ba1\u7406\uff1b\u800cReentrantLock\u9700\u8981\u663e\u5f0f\u8c03\u7528unlock\u65b9\u6cd5\u6765\u91ca\u653e\u9501\uff0c\u8fd9\u6837\u5c31\u80fd\u591f\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u9501\u7684\u91ca\u653e\u65f6\u673a\u3002<\/span><\/p>\n<\/li>\n<\/ol>\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 15px;\">\u4e3e\u4f8b\u8bf4\u660e\uff1a<\/span><\/strong><span style=\"font-size: 15px;\"><\/span><\/section>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"cs\"><code><span class=\"code-snippet_outer\">import java.util.concurrent.locks.ReentrantLock;<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">ReentrantLockExample<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__keyword\">private<\/span> final ReentrantLock <span class=\"code-snippet__keyword\">lock<\/span> = <span class=\"code-snippet__keyword\">new<\/span> ReentrantLock();<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">performTask<\/span>(<span class=\"code-snippet__params\"><\/span>)<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">lock<\/span>.<span class=\"code-snippet__keyword\">lock<\/span>(); <span class=\"code-snippet__comment\">\/\/ \u83b7\u53d6\u9501<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">try<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__comment\">\/\/ \u53d7\u4fdd\u62a4\u7684\u4e34\u754c\u533a\u57df<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__comment\">\/\/ ...<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">            performSubTask(); <span class=\"code-snippet__comment\">\/\/ \u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u518d\u6b21\u83b7\u53d6\u9501\uff0c\u4e0d\u4f1a\u88ab\u963b\u585e<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__comment\">\/\/ ...<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">        } <span class=\"code-snippet__keyword\">finally<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__keyword\">lock<\/span>.unlock(); <span class=\"code-snippet__comment\">\/\/ \u91ca\u653e\u9501<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">        }<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">performSubTask<\/span>(<span class=\"code-snippet__params\"><\/span>)<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">lock<\/span>.<span class=\"code-snippet__keyword\">lock<\/span>(); <span class=\"code-snippet__comment\">\/\/ \u518d\u6b21\u83b7\u53d6\u540c\u4e00\u4e2a\u9501\uff0c\u4e0d\u4f1a\u88ab\u963b\u585e<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">try<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__comment\">\/\/ \u53d7\u4fdd\u62a4\u7684\u4e34\u754c\u533a\u57df<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__comment\">\/\/ ...<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">        } <span class=\"code-snippet__keyword\">finally<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__keyword\">lock<\/span>.unlock(); <span class=\"code-snippet__comment\">\/\/ \u91ca\u653e\u9501<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">        }<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/pre>\n<\/section>\n<p data-v-md-line=\"15\"><span style=\"font-size: 15px;\">\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0cperformTask\u65b9\u6cd5\u548cperformSubTask\u65b9\u6cd5\u90fd\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u591a\u6b21\u83b7\u53d6\u4e86\u540c\u4e00\u4e2aReentrantLock\uff0c\u800c\u4e0d\u4f1a\u9020\u6210<strong>\u6b7b\u9501\u6216\u963b\u585e<\/strong>\u3002\u8fd9\u5c55\u793a\u4e86\u91cd\u5165\u9501\u7684\u7279\u6027\u3002<\/span><span style=\"font-size: 15px;letter-spacing: 0.034em;\"><\/span><\/p>\n<p style=\"display: none;\"><mp-style-type data-value=\"3\"><\/mp-style-type><\/p>\n<blockquote>\n<p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7:                 \u4e92\u8054\u7f51\u9762\u8bd5\u5c0f\u5e2e\u624b              <\/p>\n<\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u91cd\u5165\u9501\uff08ReentrantLock\uff09\u662fJava\u4e2d\u7684\u4e00\u79cd\u663e\u5f0f\u9501\u5b9e\u73b0\uff0c\u5b83\u5141\u8bb8\u7ebf\u7a0b\u91cd\u590d\u83b7\u53d6\u540c\u4e00\u4e2a\u9501\u3002\u8fd9\u610f\u5473\u7740\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u591a\u6b21\u83b7\u5f97\u540c\u4e00\u4e2a\u9501 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4109,"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-4112","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\/20240524145221194-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145221194-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145221194.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145221194-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145221194.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145221194.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-04-16","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":362,"next_post_id":4623,"next_post_title":"\u5403\u900fRedis\u7cfb\u5217\uff1a\u9ad8\u53ef\u7528\u7684\u54e8\u5175\u6a21\u5f0f","previous_post_id":4116,"previous_post_title":"\u9762\u8bd5\u5b98-\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0cHashMap \u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u6b7b\u5faa\u73af\uff1f","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4112","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=4112"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4112\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4109"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}