{"id":4116,"date":"2024-04-15T09:10:53","date_gmt":"2024-04-15T01:10:53","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%8e%af%e5%a2%83%e4%b8%8b%ef%bc%8chashmap-%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0%e6%ad%bb%e5%be%aa%e7%8e%af%ef%bc%9f\/"},"modified":"2024-04-15T09:10:53","modified_gmt":"2024-04-15T01:10:53","slug":"%e9%9d%a2%e8%af%95%e5%ae%98-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%8e%af%e5%a2%83%e4%b8%8b%ef%bc%8chashmap-%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0%e6%ad%bb%e5%be%aa%e7%8e%af%ef%bc%9f","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-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%8e%af%e5%a2%83%e4%b8%8b%ef%bc%8chashmap-%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0%e6%ad%bb%e5%be%aa%e7%8e%af%ef%bc%9f\/","title":{"rendered":"\u9762\u8bd5\u5b98-\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0cHashMap \u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u6b7b\u5faa\u73af\uff1f"},"content":{"rendered":"<div class='wxsyncmain'>\n<p data-v-md-line=\"3\"><span style=\"font-size: 15px;\">\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0cHashMap \u53ef\u80fd\u51fa\u73b0\u6b7b\u5faa\u73af\uff0c\u539f\u56e0\u662f\u6876\u94fe\u8868\u4e2d\u5b58\u5728\u5faa\u73af\u5f15\u7528\u3002<\/span><\/p>\n<p data-v-md-line=\"5\"><strong><span style=\"font-size: 18px;\">\u6876\u94fe\u8868<\/span><\/strong><\/p>\n<p data-v-md-line=\"7\"><span style=\"font-size: 15px;\">HashMap \u4f7f\u7528\u6876\u94fe\u8868\u6765\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u6bcf\u4e2a\u6876\u94fe\u8868\u90fd\u5b58\u50a8\u7740\u6240\u6709\u54c8\u5e0c\u5230\u540c\u4e00\u6876\u7684\u952e\u503c\u5bf9\u3002\u5f53\u4e00\u4e2a\u952e\u88ab\u63d2\u5165\u5230 HashMap \u65f6\uff0c\u5b83\u7684\u54c8\u5e0c\u503c\u88ab\u8ba1\u7b97\u51fa\u6765\uff0c\u7528\u6765\u786e\u5b9a\u63d2\u5165\u54ea\u4e2a\u6876\u94fe\u8868\u3002<\/span><\/p>\n<p data-v-md-line=\"9\"><strong><span style=\"font-size: 18px;\">\u5faa\u73af\u5f15\u7528<\/span><\/strong><\/p>\n<p data-v-md-line=\"11\"><span style=\"font-size: 15px;\">\u5982\u679c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5c1d\u8bd5\u63d2\u5165\u6216\u5220\u9664\u5143\u7d20\u5230\u540c\u4e00\u6876\u94fe\u8868\uff0c\u5219\u53ef\u80fd\u4f1a\u51fa\u73b0\u5faa\u73af\u5f15\u7528\u3002\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u521b\u5efa\u4e86\u4e00\u4e2a\u4ece\u5934\u7ed3\u70b9\u6307\u5411\u67d0\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff0c\u800c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4ece\u8be5\u8282\u70b9\u6307\u5411\u5934\u7ed3\u70b9\u3002\u8fd9\u4f1a\u5bfc\u81f4\u4e00\u4e2a\u5faa\u73af\u5f15\u7528\uff0c\u5bfc\u81f4\u7ebf\u7a0b\u6c38\u8fdc\u65e0\u6cd5\u5b8c\u6210\u64cd\u4f5c\u3002<\/span><\/p>\n<p data-v-md-line=\"13\"><strong><span style=\"font-size: 18px;\">\u6b7b\u5faa\u73af\u793a\u4f8b<\/span><\/strong><\/p>\n<p data-v-md-line=\"15\"><span style=\"font-size: 15px;\">\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5bfc\u81f4 HashMap \u6b7b\u5faa\u73af\u7684\u793a\u4f8b\uff1a<\/span><\/p>\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=\"cpp\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> java.util.HashMap;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> java.util.Map;<\/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__class\"><span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">HashMapDeadlock<\/span> {<\/span><\/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\">static<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">main<\/span><span class=\"code-snippet__params\">(String[] args)<\/span> <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">        Map&lt;Integer, Integer&gt; <span class=\"code-snippet__built_in\">map<\/span> = <span class=\"code-snippet__keyword\">new<\/span> HashMap&lt;&gt;();<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\">        Thread thread1 = <span class=\"code-snippet__keyword\">new<\/span> Thread(() -&gt; {<\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__built_in\">map<\/span>.put(<span class=\"code-snippet__number\">1<\/span>, <span class=\"code-snippet__number\">2<\/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\">        Thread thread2 = <span class=\"code-snippet__keyword\">new<\/span> Thread(() -&gt; {<\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__built_in\">map<\/span>.remove(<span class=\"code-snippet__number\">1<\/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\">        thread1.start();<\/span><\/code><code><span class=\"code-snippet_outer\">        thread2.start();<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">try<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">            thread1.join();<\/span><\/code><code><span class=\"code-snippet_outer\">            thread2.join();<\/span><\/code><code><span class=\"code-snippet_outer\">        } <span class=\"code-snippet__keyword\">catch<\/span> (InterruptedException e) {<\/span><\/code><code><span class=\"code-snippet_outer\">            e.printStackTrace();<\/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=\"47\"><span style=\"font-size: 15px;\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5c1d\u8bd5\u5bf9\u952e 1 \u8fdb\u884c\u64cd\u4f5c\u3002<\/span><\/p>\n<ul data-v-md-line=\"49\" class=\"list-paddingleft-1\" style=\"\">\n<li>\n<p><span style=\"font-size: 15px;\">\u7ebf\u7a0b 1 \u8bd5\u56fe\u5c06\u952e 1 \u6620\u5c04\u5230\u503c 2\uff0c\u8fd9\u4f1a\u521b\u5efa\u4e00\u4e2a\u4ece\u5934\u7ed3\u70b9\u6307\u5411\u65b0\u521b\u5efa\u8282\u70b9\u7684\u5f15\u7528\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"font-size: 15px;\">\u7ebf\u7a0b 2 \u8bd5\u56fe\u5220\u9664\u952e 1\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u8be5\u8282\u70b9\u4ece\u6876\u94fe\u8868\u4e2d\u5220\u9664\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"font-size: 15px;\">\u4f46\u662f\uff0c\u7531\u4e8e\u7ebf\u7a0b 1 \u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u6307\u5411\u8be5\u8282\u70b9\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u5bfc\u81f4\u4e86\u5faa\u73af\u5f15\u7528\u3002<\/span><\/p>\n<\/li>\n<li style=\"\">\n<p><span style=\"font-size: 15px;\">\u7531\u4e8e\u5b58\u5728\u5faa\u73af\u5f15\u7528\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u4f1a\u65e0\u9650\u671f\u5730\u7b49\u5f85\u5bf9\u65b9\u5b8c\u6210\u64cd\u4f5c\uff0c\u4ece\u800c\u5bfc\u81f4\u6b7b\u5faa\u73af\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 18px;\">\u9632\u6b62\u6b7b\u5faa\u73af<\/span><\/strong><\/section>\n<p data-v-md-line=\"56\"><span style=\"font-size: 15px;\">\u4e3a\u4e86\u9632\u6b62 HashMap \u6b7b\u5faa\u73af\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\uff1a<\/span><\/p>\n<ul data-v-md-line=\"58\" class=\"list-paddingleft-1\">\n<li>\n<p><strong><span style=\"font-size: 15px;\">\u4f7f\u7528\u5e76\u53d1\u5bb9\u5668<\/span><\/strong><span style=\"font-size: 15px;\">\uff1aJava 5 \u53ca\u66f4\u9ad8\u7248\u672c\u63d0\u4f9b\u4e86 ConcurrentHashMap\uff0c\u5b83\u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684 HashMap \u5b9e\u73b0\uff0c\u53ef\u4ee5\u9632\u6b62\u6b7b\u5faa\u73af\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><strong><span style=\"font-size: 15px;\">\u540c\u6b65\u8bbf\u95ee<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u5982\u679c\u5fc5\u987b\u4f7f\u7528 HashMap\uff0c\u53ef\u4ee5\u5bf9\u6574\u4e2a HashMap \u6216\u7279\u5b9a\u7684\u6876\u94fe\u8868\u8fdb\u884c\u540c\u6b65\u3002\u4f46\u662f\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><strong><span style=\"font-size: 15px;\">\u6ce8\u610f\u6570\u636e\u7ed3\u6784<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u907f\u514d\u5728 HashMap \u4e2d\u5b58\u50a8\u5f15\u7528\u5faa\u73af\u7684\u5bf9\u8c61\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6b7b\u5faa\u73af\u3002<\/span><\/p>\n<\/li>\n<\/ul>\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>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0cHashMap \u53ef\u80fd\u51fa\u73b0\u6b7b\u5faa\u73af\uff0c\u539f\u56e0\u662f\u6876\u94fe\u8868\u4e2d\u5b58\u5728\u5faa\u73af\u5f15\u7528\u3002 \u6876\u94fe\u8868 HashMap \u4f7f\u7528\u6876\u94fe\u8868\u6765\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u6bcf\u4e2a &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4113,"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-4116","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\/20240524145256966-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145256966-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145256966.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145256966-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145256966.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145256966.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-04-15","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":298,"next_post_id":4112,"next_post_title":"\u5b57\u8282\u9762\u8bd5\u9898\uff1a\u4ec0\u4e48\u662f\u91cd\u5165\u9501\uff08ReentrantLock\uff09\uff1f\u4e0esynchronized\u5173\u952e\u5b57\u7684\u533a\u522b\uff1f","previous_post_id":4120,"previous_post_title":"\u9762\u8bd5\u5b98-\u4e3a\u4ec0\u4e48 ThreadLocal \u53ef\u4ee5\u505a\u5230\u7ebf\u7a0b\u9694\u79bb\uff1f","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4116","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=4116"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4116\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4113"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}